|
| 1 | +// Copyright 2020 The Lokomotive Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// +build aks aws aws_edge packet |
| 16 | +// +build e2e |
| 17 | + |
| 18 | +//nolint |
| 19 | +package components |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + _ "github.com/kinvolk/lokomotive/pkg/components/flatcar-linux-update-operator" |
| 28 | + testutil "github.com/kinvolk/lokomotive/test/components/util" |
| 29 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + "k8s.io/apimachinery/pkg/util/wait" |
| 31 | + "k8s.io/client-go/kubernetes" |
| 32 | +) |
| 33 | + |
| 34 | +const ( |
| 35 | + retryInterval = time.Second * 5 |
| 36 | + timeout = time.Minute * 5 |
| 37 | + contextTimeout = 10 |
| 38 | +) |
| 39 | + |
| 40 | +type componentTestCase struct { |
| 41 | + namespace string |
| 42 | + platforms []testutil.Platform |
| 43 | +} |
| 44 | + |
| 45 | +func TestDisableAutomountServiceAccountToken(t *testing.T) { |
| 46 | + client := testutil.CreateKubeClient(t) |
| 47 | + componentTestCases := []componentTestCase{ |
| 48 | + { |
| 49 | + namespace: "cert-manager", |
| 50 | + platforms: []testutil.Platform{testutil.PlatformPacket, testutil.PlatformAWSEdge, testutil.PlatformAWS, testutil.PlatformAKS}, |
| 51 | + }, |
| 52 | + { |
| 53 | + namespace: "projectcontour", |
| 54 | + platforms: []testutil.Platform{testutil.PlatformPacket, testutil.PlatformAWSEdge, testutil.PlatformAWS, testutil.PlatformAKS}, |
| 55 | + }, |
| 56 | + { |
| 57 | + namespace: "dex", |
| 58 | + platforms: []testutil.Platform{testutil.PlatformPacket, testutil.PlatformAWSEdge, testutil.PlatformAWS}, |
| 59 | + }, |
| 60 | + { |
| 61 | + namespace: "external-dns", |
| 62 | + platforms: []testutil.Platform{testutil.PlatformPacket, testutil.PlatformAWSEdge, testutil.PlatformAWS, testutil.PlatformAKS}, |
| 63 | + }, |
| 64 | + { |
| 65 | + namespace: "reboot-coordinator", |
| 66 | + platforms: []testutil.Platform{testutil.PlatformPacket, testutil.PlatformAWSEdge, testutil.PlatformAWS}, |
| 67 | + }, |
| 68 | + { |
| 69 | + namespace: "gangway", |
| 70 | + platforms: []testutil.Platform{testutil.PlatformPacket, testutil.PlatformAWSEdge, testutil.PlatformAWS}, |
| 71 | + }, |
| 72 | + { |
| 73 | + namespace: "httpbin", |
| 74 | + platforms: []testutil.Platform{testutil.PlatformPacket, testutil.PlatformAWSEdge, testutil.PlatformAWS, testutil.PlatformAKS}, |
| 75 | + }, |
| 76 | + { |
| 77 | + namespace: "metallb-system", |
| 78 | + platforms: []testutil.Platform{testutil.PlatformPacket, testutil.PlatformAWSEdge}, |
| 79 | + }, |
| 80 | + { |
| 81 | + namespace: "openebs", |
| 82 | + platforms: []testutil.Platform{testutil.PlatformPacket, testutil.PlatformAWSEdge, testutil.PlatformAWS}, |
| 83 | + }, |
| 84 | + { |
| 85 | + namespace: "monitoring", |
| 86 | + platforms: []testutil.Platform{testutil.PlatformPacket, testutil.PlatformAWSEdge, testutil.PlatformAWS}, |
| 87 | + }, |
| 88 | + { |
| 89 | + namespace: "rook", |
| 90 | + platforms: []testutil.Platform{testutil.PlatformPacket, testutil.PlatformAWSEdge, testutil.PlatformAWS}, |
| 91 | + }, |
| 92 | + } |
| 93 | + |
| 94 | + for _, tc := range componentTestCases { |
| 95 | + tc := tc |
| 96 | + t.Run(tc.namespace, func(t *testing.T) { |
| 97 | + t.Parallel() |
| 98 | + |
| 99 | + if !testutil.IsPlatformSupported(t, tc.platforms) { |
| 100 | + t.Skip() |
| 101 | + } |
| 102 | + |
| 103 | + if err := wait.PollImmediate( |
| 104 | + retryInterval, timeout, checkDefaultServiceAccountPatch(client, tc), |
| 105 | + ); err != nil { |
| 106 | + t.Fatalf("%v", err) |
| 107 | + } |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func checkDefaultServiceAccountPatch(client kubernetes.Interface, tc componentTestCase) wait.ConditionFunc { |
| 113 | + return func() (done bool, err error) { |
| 114 | + ctx, cancel := context.WithTimeout(context.Background(), contextTimeout*time.Second) |
| 115 | + defer cancel() |
| 116 | + |
| 117 | + sa, err := client.CoreV1().ServiceAccounts(tc.namespace).Get(ctx, "default", metav1.GetOptions{}) |
| 118 | + if err != nil { |
| 119 | + return false, fmt.Errorf("error getting service account: %v", err) |
| 120 | + } |
| 121 | + |
| 122 | + automountServiceAccountToken := *sa.AutomountServiceAccountToken |
| 123 | + |
| 124 | + if automountServiceAccountToken != false { |
| 125 | + //nolint:lll |
| 126 | + return false, fmt.Errorf("service account for namespace %q was not patched. Expected %v got %v", tc.namespace, false, automountServiceAccountToken) |
| 127 | + } |
| 128 | + |
| 129 | + return true, nil |
| 130 | + } |
| 131 | +} |
0 commit comments