Skip to content

Commit

Permalink
Update test go
Browse files Browse the repository at this point in the history
  • Loading branch information
micahhausler committed Nov 9, 2023
1 parent 566a812 commit da5341d
Show file tree
Hide file tree
Showing 31 changed files with 707 additions and 1,367 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
with:
show-progress: false
- name: "Dependency Review"
uses: actions/dependency-review-action@v3
uses: actions/dependency-review-action@v3.1.2
govulncheck:
runs-on: ubuntu-latest
steps:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ aws-iam-authenticator
test-artifacts
hack/e2e/e2e-test-artifacts

go.work
go.work.sum

# == kubernetes .gitignore ==

# OSX leaves these everywhere on SMB shares
Expand Down
7 changes: 4 additions & 3 deletions cmd/aws-iam-authenticator/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ var serverCmd = &cobra.Command{
var err error
fmt.Printf("Authenticator Version: %q, %q\n", pkg.Version, pkg.CommitID)
metrics.InitMetrics(prometheus.DefaultRegisterer)
stopCh := signals.SetupSignalHandler()
cmd.Context()
ctx := signals.SetupSignalHandler()

cfg, err := getConfig()
if err != nil {
logrus.Fatalf("%s", err)
}

httpServer := server.New(cfg, stopCh)
httpServer.Run(stopCh)
httpServer := server.New(ctx, cfg, )
httpServer.Run(ctx)
},
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ require (
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJY
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
Expand Down
6 changes: 3 additions & 3 deletions hack/e2e/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ ECR_URL=${AWS_ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com
IMAGE_NAME=${IMAGE_NAME:-${ECR_URL}/${REPO_NAME}}
IMAGE_TAG=${IMAGE_TAG:-${TEST_ID}}

K8S_VERSION=${K8S_VERSION:-1.22.10}
K8S_VERSION=${K8S_VERSION:-1.28.3}

KOPS_VERSION=${KOPS_VERSION:-1.23.0}
KOPS_VERSION=${KOPS_VERSION:-1.28.0}

KOPS_STATE_FILE=${KOPS_STATE_FILE:-s3://${KOPS_STATE_BUCKET}}
KOPS_PATCH_FILE=${KOPS_PATCH_FILE:-${BASE_DIR}/kops-patch.yaml}
Expand All @@ -66,7 +66,7 @@ ROLES_SPEC_FILE=${ROLES_SPEC_FILE:-${BASE_DIR}/roles.yaml}
APISERVER_UPDATE_FILE=${APISERVER_UPDATE_FILE:-${BASE_DIR}/apiserver-update.yaml}
KUBECONFIG_UPDATE_FILE=${KUBECONFIG_UPDATE_FILE:-${BASE_DIR}/kubeconfig-update.yaml}

KUBECTL_VERSION=${KUBECTL_VERSION:-1.24.0}
KUBECTL_VERSION=${KUBECTL_VERSION:-1.28.3}

TEST_PATH=${TEST_PATH:-"./..."}
ARTIFACTS=${ARTIFACTS:-"${TEST_DIR}/artifacts"}
Expand Down
2 changes: 1 addition & 1 deletion hack/test-integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ EOF
CREATE_TEST_ROLE="${CREATE_TEST_ROLE:-true}"
GENERATED_TEST_ROLE_NAME="aws-iam-authenticator-test-role-${RANDOM}"
GENERATED_TEST_ROLE_POLICY_FILE=/tmp/role-policy.json
KUBERNETES_TAG="v1.22.1"
KUBERNETES_TAG="v1.28.3"
REPO_ROOT="$(cd "$( dirname "${BASH_SOURCE[0]}" )"/.. &> /dev/null && pwd)"
TEST_ARTIFACTS="${TEST_ARTIFACTS:-"${REPO_ROOT}/test-artifacts"}"
TEST_ROLE_ARN="${TEST_ROLE_ARN:-$(role_arn_from_default_credentials)}"
Expand Down
7 changes: 4 additions & 3 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ set -o nounset
set -o pipefail

SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${SCRIPT_ROOT}"; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ../code-generator)}
chmod +x ${CODEGEN_PKG}/generate-groups.sh
CODE_GEN_VERSION=$(go mod edit -print |grep 'k8s.io/code-generator' | cut -f2 -d' ')
CODEGEN_PKG=${CODEGEN_PKG:-$GOPATH/pkg/mod/k8s.io/code-generator\@${CODE_GEN_VERSION}}
chmod +x ${CODEGEN_PKG}/kube_codegen.sh
# generate the code with:
# --output-base because this script should also be able to run inside the vendor dir of
# k8s.io/kubernetes. The output-base is needed for the generators to output into the vendor dir
# instead of the $GOPATH directly. For normal projects this can be dropped.
"${CODEGEN_PKG}"/generate-groups.sh "deepcopy,client,informer,lister" \
"${CODEGEN_PKG}"/kube_codegen.sh "deepcopy,client,informer,lister" \
sigs.k8s.io/aws-iam-authenticator/pkg/mapper/crd/generated sigs.k8s.io/aws-iam-authenticator/pkg/mapper/crd/apis \
iamauthenticator:v1alpha1 \
--output-base "$(dirname "${BASH_SOURCE[0]}")/../../.." \
Expand Down
33 changes: 17 additions & 16 deletions pkg/fileutil/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fileutil

import (
"context"
"os"
"time"

Expand All @@ -11,20 +12,20 @@ import (
)

type FileChangeCallBack interface {
CallBackForFileLoad(dynamicContent []byte) error
CallBackForFileDeletion() error
CallBackForFileLoad(ctx context.Context, dynamicContent []byte) error
CallBackForFileDeletion(context.Context) error
}

func waitUntilFileAvailable(filename string, stopCh <-chan struct{}) {
func waitUntilFileAvailable(ctx context.Context, filename string) {
if _, err := os.Stat(filename); err == nil {
return
}
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for {
select {
case <-stopCh:
logrus.Infof("startLoadDynamicFile: waitUntilFileAvailable exit because get stopCh, filename is %s", filename)
case <-ctx.Done():
logrus.Infof("startLoadDynamicFile: waitUntilFileAvailable exit because context completed, filename is %s", filename)
return
case <-ticker.C:
if _, err := os.Stat(filename); err == nil {
Expand All @@ -34,8 +35,8 @@ func waitUntilFileAvailable(filename string, stopCh <-chan struct{}) {
}
}

func loadDynamicFile(filename string, stopCh <-chan struct{}) ([]byte, error) {
waitUntilFileAvailable(filename, stopCh)
func loadDynamicFile(ctx context.Context, filename string) ([]byte, error) {
waitUntilFileAvailable(ctx, filename)
if content, err := os.ReadFile(filename); err == nil {
logrus.Infof("LoadDynamicFile: %v is available. content is %s", filename, string(content))
return content, nil
Expand All @@ -44,7 +45,7 @@ func loadDynamicFile(filename string, stopCh <-chan struct{}) ([]byte, error) {
}
}

func StartLoadDynamicFile(filename string, callBack FileChangeCallBack, stopCh <-chan struct{}) {
func StartLoadDynamicFile(ctx context.Context, filename string, callBack FileChangeCallBack) {
go wait.Until(func() {
// start to watch the file change
watcher, err := fsnotify.NewWatcher()
Expand All @@ -54,7 +55,7 @@ func StartLoadDynamicFile(filename string, callBack FileChangeCallBack, stopCh <
return
}
defer watcher.Close()
content, err := loadDynamicFile(filename, stopCh)
content, err := loadDynamicFile(ctx, filename)
if err != nil {
return
}
Expand All @@ -64,33 +65,33 @@ func StartLoadDynamicFile(filename string, callBack FileChangeCallBack, stopCh <
metrics.Get().DynamicFileFailures.Inc()
return
}
if err := callBack.CallBackForFileLoad(content); err != nil {
if err := callBack.CallBackForFileLoad(ctx, content); err != nil {
logrus.Errorf("StartLoadDynamicFile: error in callBackForFileLoad, %v", err)
}
for {
select {
case <-stopCh:
logrus.Infof("startLoadDynamicFile: watching exit because stopCh closed, filename is %s", filename)
case <-ctx.Done():
logrus.Infof("startLoadDynamicFile: watching exit because context completed, filename is %s", filename)
return
case event := <-watcher.Events:
switch {
case event.Op&fsnotify.Write == fsnotify.Write, event.Op&fsnotify.Create == fsnotify.Create:
// reload the access entry file
logrus.Info("startLoadDynamicFile: got WRITE/CREATE event reload it the memory")
content, err := loadDynamicFile(filename, stopCh)
content, err := loadDynamicFile(ctx, filename)
if err != nil {
logrus.Errorf("StartLoadDynamicFile: error in loadDynamicFile, %v", err)
return
}
if err := callBack.CallBackForFileLoad(content); err != nil {
if err := callBack.CallBackForFileLoad(ctx, content); err != nil {
logrus.Errorf("StartLoadDynamicFile: error in callBackForFileLoad, %v", err)
}
case event.Op&fsnotify.Rename == fsnotify.Rename, event.Op&fsnotify.Remove == fsnotify.Remove:
logrus.Info("startLoadDynamicFile: got RENAME/REMOVE event")
// test if the "REMOVE" is triggered by vi or cp cmd
_, err := os.Stat(filename)
if os.IsNotExist(err) {
if err := callBack.CallBackForFileDeletion(); err != nil {
if err := callBack.CallBackForFileDeletion(ctx); err != nil {
logrus.Errorf("StartLoadDynamicFile: error in callBackForFileDeletion, %v", err)
}
}
Expand All @@ -102,5 +103,5 @@ func StartLoadDynamicFile(filename string, callBack FileChangeCallBack, stopCh <
return
}
}
}, time.Second, stopCh)
}, time.Second, ctx.Done())
}
18 changes: 10 additions & 8 deletions pkg/fileutil/util_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fileutil

import (
"context"
"os"
"sync"
"testing"
Expand All @@ -21,14 +22,14 @@ type testStruct struct {
mutex sync.Mutex
}

func (a *testStruct) CallBackForFileLoad(dynamicContent []byte) error {
func (a *testStruct) CallBackForFileLoad(ctx context.Context, dynamicContent []byte) error {
a.mutex.Lock()
a.expectedContent = string(dynamicContent)
defer a.mutex.Unlock()
return nil
}

func (a *testStruct) CallBackForFileDeletion() error {
func (a *testStruct) CallBackForFileDeletion(ctx context.Context) error {
a.mutex.Lock()
a.expectedContent = ""
defer a.mutex.Unlock()
Expand Down Expand Up @@ -57,10 +58,11 @@ func TestLoadDynamicFile(t *testing.T) {
"eks:test",
},
}
stopCh := make(chan struct{})

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
testA := &testStruct{}
StartLoadDynamicFile("/tmp/util_test.txt", testA, stopCh)
defer close(stopCh)
StartLoadDynamicFile(ctx, "/tmp/util_test.txt", testA)
time.Sleep(2 * time.Second)
os.WriteFile("/tmp/util_test.txt", []byte("test"), 0777)
for {
Expand Down Expand Up @@ -98,10 +100,10 @@ func updateFile(testA *testStruct, origFileContent string, t *testing.T) {
}

func TestDeleteDynamicFile(t *testing.T) {
stopCh := make(chan struct{})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
testA := &testStruct{}
StartLoadDynamicFile("/tmp/delete.txt", testA, stopCh)
defer close(stopCh)
StartLoadDynamicFile(ctx, "/tmp/delete.txt", testA)
time.Sleep(2 * time.Second)
os.WriteFile("/tmp/delete.txt", []byte("test"), 0777)
for {
Expand Down
8 changes: 4 additions & 4 deletions pkg/mapper/configmap/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ func New(masterURL, kubeConfig string) (*MapStore, error) {

// Starts a go routine which will watch the configmap and update the in memory data
// when the values change.
func (ms *MapStore) startLoadConfigMap(stopCh <-chan struct{}) {
func (ms *MapStore) startLoadConfigMap(ctx context.Context) {
go func() {
logrus.Info("startLoadConfigMap in EKSConfigMap")
for {
select {
case <-stopCh:
logrus.Info("stopCh is closed in startLoadConfigMap")
case <-ctx.Done():
logrus.Info("Context is complete in startLoadConfigMap")
return
default:
watcher, err := ms.configMap.Watch(context.TODO(), metav1.ListOptions{
watcher, err := ms.configMap.Watch(ctx, metav1.ListOptions{
Watch: true,
FieldSelector: fields.OneTermEqualSelector("metadata.name", "aws-auth").String(),
})
Expand Down
7 changes: 4 additions & 3 deletions pkg/mapper/configmap/configmap_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package configmap

import (
"context"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -182,9 +183,9 @@ func TestLoadConfigMap(t *testing.T) {
return true, watcher, nil
})

stopCh := make(chan struct{})
ms.startLoadConfigMap(stopCh)
defer close(stopCh)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ms.startLoadConfigMap(ctx)

time.Sleep(2 * time.Second)

Expand Down
8 changes: 5 additions & 3 deletions pkg/mapper/configmap/mapper.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package configmap

import (
"sigs.k8s.io/aws-iam-authenticator/pkg/token"
"context"
"strings"

"sigs.k8s.io/aws-iam-authenticator/pkg/token"

"sigs.k8s.io/aws-iam-authenticator/pkg/config"
"sigs.k8s.io/aws-iam-authenticator/pkg/mapper"
)
Expand All @@ -26,8 +28,8 @@ func (m *ConfigMapMapper) Name() string {
return mapper.ModeEKSConfigMap
}

func (m *ConfigMapMapper) Start(stopCh <-chan struct{}) error {
m.startLoadConfigMap(stopCh)
func (m *ConfigMapMapper) Start(ctx context.Context) error {
m.startLoadConfigMap(ctx)
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/mapper/configmap/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ func TestConfigMap(t *testing.T) {
ms := MapStore{}
ms.configMap = cs.CoreV1().ConfigMaps("kube-system")

stopCh := make(chan struct{})
ms.startLoadConfigMap(stopCh)
defer close(stopCh)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ms.startLoadConfigMap(ctx)

time.Sleep(10 * time.Millisecond)
_, _ = cs.CoreV1().ConfigMaps("kube-system").Create(context.TODO(), cm, metav1.CreateOptions{})
Expand Down
8 changes: 4 additions & 4 deletions pkg/mapper/crd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,24 @@ func New(
}

// Run will implement the loop for processing items
func (c *Controller) Run(threadiness int, stopCh <-chan struct{}) error {
func (c *Controller) Run(ctx context.Context, threadiness int) error {
defer utilruntime.HandleCrash()
defer c.workqueue.ShutDown()

logrus.Info("starting aws iam authenticator controller")

logrus.Info("waiting for informer caches to sync")
if ok := cache.WaitForCacheSync(stopCh, c.iamMappingsSynced); !ok {
if ok := cache.WaitForCacheSync(ctx.Done(), c.iamMappingsSynced); !ok {
return fmt.Errorf("failed to wait for caches to sync")
}

logrus.Info("starting workers")
for i := 0; i < threadiness; i++ {
go wait.Until(c.runWorker, time.Second, stopCh)
go wait.Until(c.runWorker, time.Second, ctx.Done())
}

logrus.Info("started workers")
<-stopCh
<-ctx.Done()
logrus.Info("shutting down workers")

return nil
Expand Down
Loading

0 comments on commit da5341d

Please sign in to comment.