From fa23d11a070be06e715985027df936b641e082a5 Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Thu, 26 Jan 2023 15:28:37 +0100 Subject: [PATCH] new(tests): ported tests to new config.Loop API. Signed-off-by: Federico Di Pierro --- main.go | 3 +- main_test.go | 152 +++++++++++++++++++++++++++++++ pkg/{poiana => config}/config.go | 30 +++--- secrets_test.go | 86 ----------------- variables_test.go | 69 -------------- 5 files changed, 165 insertions(+), 175 deletions(-) create mode 100644 main_test.go rename pkg/{poiana => config}/config.go (90%) delete mode 100644 secrets_test.go delete mode 100644 variables_test.go diff --git a/main.go b/main.go index bb8b5c4..31ad191 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "context" "flag" + "github.com/FedeDP/GhEnvSet/pkg/config" "github.com/FedeDP/GhEnvSet/pkg/poiana" "github.com/sirupsen/logrus" ) @@ -39,7 +40,7 @@ func initOpts() { func main() { initOpts() - conf, err := poiana.FromFile(confFile) + conf, err := config.FromFile(confFile) if err != nil { logrus.Fatal(err) } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..1efb3d7 --- /dev/null +++ b/main_test.go @@ -0,0 +1,152 @@ +package main + +import ( + "context" + "encoding/base64" + "github.com/FedeDP/GhEnvSet/pkg/config" + "github.com/FedeDP/GhEnvSet/pkg/poiana" + "github.com/google/go-github/v49/github" + "github.com/stretchr/testify/assert" + "testing" +) + +const testYAML = ` +orgs: + FedeDP: + repos: + GhEnvSet: + actions: + variables: + var1: "value1" + var2: "value2" + secrets: + - secret0 + - secret1 + - secret2 +` + +type MockVariableService struct { + variables map[string]string +} + +func (m MockVariableService) ListRepoVariables(ctx context.Context, owner, repo string) (*poiana.Variables, error) { + vars := make([]*poiana.Variable, 0) + for key, val := range m.variables { + vars = append(vars, &poiana.Variable{ + Name: key, + Value: val, + }) + } + + return &poiana.Variables{ + TotalCount: len(m.variables), + Variables: vars, + }, nil +} + +func (m MockVariableService) DeleteRepoVariable(ctx context.Context, owner, repo, name string) error { + delete(m.variables, name) + return nil +} + +func (m MockVariableService) CreateOrUpdateRepoVariable(ctx context.Context, owner, repo string, variable *poiana.Variable) error { + m.variables[variable.Name] = variable.Value + return nil +} + +func newMockVariableService() poiana.ActionsVarsService { + mServ := &MockVariableService{variables: make(map[string]string, 0)} + // Initial variable set + _ = mServ.CreateOrUpdateRepoVariable(context.Background(), "", "", &poiana.Variable{ + Name: "var0", + Value: "value0", + CreatedAt: github.Timestamp{}, + UpdatedAt: github.Timestamp{}, + }) + return mServ +} + +type MockSecretsService struct { + secrets map[string]*github.EncryptedSecret +} + +func (m MockSecretsService) ListRepoSecrets(ctx context.Context, owner, repo string) (*github.Secrets, error) { + secs := make([]*github.Secret, 0) + for key, _ := range m.secrets { + secs = append(secs, &github.Secret{ + Name: key, + }) + } + + return &github.Secrets{ + TotalCount: len(m.secrets), + Secrets: secs, + }, nil +} + +func (m MockSecretsService) DeleteRepoSecret(ctx context.Context, owner, repo, name string) error { + delete(m.secrets, name) + return nil +} + +func (m MockSecretsService) CreateOrUpdateRepoSecret(ctx context.Context, owner, repo string, eSecret *github.EncryptedSecret) error { + m.secrets[eSecret.Name] = eSecret + return nil +} + +func newMockSecretsService() poiana.ActionsSecretsService { + mServ := &MockSecretsService{secrets: make(map[string]*github.EncryptedSecret, 0)} + _ = mServ.CreateOrUpdateRepoSecret(context.Background(), "", "", &github.EncryptedSecret{ + Name: "secret0", + KeyID: "testing", + }) + return mServ +} + +type MockPublicKeyProvider struct{} + +func (pk *MockPublicKeyProvider) GetPublicKey(ctx context.Context, orgName string, repoName string) (*github.PublicKey, error) { + keyID := "testing" + key := base64.StdEncoding.EncodeToString([]byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) // 32B key + pKey := github.PublicKey{ + KeyID: &keyID, + Key: &key, + } + return &pKey, nil +} + +func newMockPublicKeyProvider() poiana.PublicKeyProvider { + return &MockPublicKeyProvider{} +} + +func TestMainLoop(t *testing.T) { + ctx := context.Background() + + conf, err := config.FromData(testYAML) + assert.NoError(t, err) + + mockVarServ := newMockVariableService() + mockSecServ := newMockSecretsService() + mockPKeyProv := newMockPublicKeyProvider() + provider, err := poiana.NewMockSecretsProvider(map[string]string{"secret0": "value0", "secret1": "value1", "secret2": "value2"}) + assert.NoError(t, err) + + err = conf.Loop(mockVarServ, mockSecServ, provider, mockPKeyProv, false) + assert.NoError(t, err) + + // Check repo variables + vars, err := mockVarServ.ListRepoVariables(ctx, "", "") + assert.NoError(t, err) + assert.Equal(t, vars.TotalCount, len(conf.Orgs["FedeDP"].Repos["GhEnvSet"].Actions.Variables)) + for _, v := range vars.Variables { + assert.Equal(t, conf.Orgs["FedeDP"].Repos["GhEnvSet"].Actions.Variables[v.Name], v.Value) + } + + // Check repo secrets + secs, err := mockSecServ.ListRepoSecrets(ctx, "", "") + assert.NoError(t, err) + assert.Equal(t, secs.TotalCount, len(conf.Orgs["FedeDP"].Repos["GhEnvSet"].Actions.Secrets)) + for _, sec := range secs.Secrets { + assert.Contains(t, conf.Orgs["FedeDP"].Repos["GhEnvSet"].Actions.Secrets, sec.Name) + } +} diff --git a/pkg/poiana/config.go b/pkg/config/config.go similarity index 90% rename from pkg/poiana/config.go rename to pkg/config/config.go index 1acc87d..ea6b9f3 100644 --- a/pkg/poiana/config.go +++ b/pkg/config/config.go @@ -1,9 +1,9 @@ -package poiana +package config import ( "context" "encoding/base64" - "io" + "github.com/FedeDP/GhEnvSet/pkg/poiana" "os" "strings" @@ -30,14 +30,6 @@ type GithubConfig struct { Orgs map[string]GitHubOrg `yaml:"orgs"` } -func (e *GithubConfig) Decode(r io.Reader) error { - return yaml.NewDecoder(r).Decode(e) -} - -func (e *GithubConfig) Encode(w io.Writer) error { - return yaml.NewEncoder(w).Encode(e) -} - func FromFile(fileName string) (*GithubConfig, error) { b, err := os.ReadFile(fileName) if err != nil { @@ -48,7 +40,7 @@ func FromFile(fileName string) (*GithubConfig, error) { func FromData(yamlData string) (*GithubConfig, error) { var conf GithubConfig - err := conf.Decode(strings.NewReader(yamlData)) + err := yaml.NewDecoder(strings.NewReader(yamlData)).Decode(&conf) if err != nil { return nil, err } @@ -56,8 +48,8 @@ func FromData(yamlData string) (*GithubConfig, error) { } func syncSecrets(ctx context.Context, - service ActionsSecretsService, - provider SecretsProvider, + service poiana.ActionsSecretsService, + provider poiana.SecretsProvider, pKey *github.PublicKey, orgName, repoName string, secrets []string) error { @@ -119,7 +111,7 @@ func syncSecrets(ctx context.Context, } func syncVariables(ctx context.Context, - service ActionsVarsService, + service poiana.ActionsVarsService, orgName, repoName string, variables map[string]string) error { @@ -145,7 +137,7 @@ func syncVariables(ctx context.Context, // Step 3: add or update all conf-listed variables for newVarName, newVarValue := range variables { logrus.Infof("adding/updating variable '%s' in repo '%s/%s'...", newVarName, orgName, repoName) - err = service.CreateOrUpdateRepoVariable(ctx, orgName, repoName, &Variable{ + err = service.CreateOrUpdateRepoVariable(ctx, orgName, repoName, &poiana.Variable{ Name: newVarName, Value: newVarValue, }) @@ -157,10 +149,10 @@ func syncVariables(ctx context.Context, } func (g *GithubConfig) Loop( - vService ActionsVarsService, - sService ActionsSecretsService, - provider SecretsProvider, - pKeyProvider PublicKeyProvider, + vService poiana.ActionsVarsService, + sService poiana.ActionsSecretsService, + provider poiana.SecretsProvider, + pKeyProvider poiana.PublicKeyProvider, dryRun bool, ) error { ctx := context.Background() diff --git a/secrets_test.go b/secrets_test.go deleted file mode 100644 index 5b7d083..0000000 --- a/secrets_test.go +++ /dev/null @@ -1,86 +0,0 @@ -package main - -const sampleYAML = ` -orgs: - FedeDP: - repos: - GhEnvSet: - actions: - variables: - SOME_VARIABLE2: "ciao" - secrets: - - TEST_SECRET_KEY -` - -// import ( -// "context" -// "encoding/base64" -// "github.com/FedeDP/GhEnvSet/pkg/poiana" -// "github.com/google/go-github/v49/github" -// "github.com/stretchr/testify/assert" -// "testing" -// ) - -// type MockSecretsService struct { -// secrets map[string]*github.EncryptedSecret -// } - -// func (m MockSecretsService) ListRepoSecrets(ctx context.Context, owner, repo string) (*github.Secrets, error) { -// secs := make([]*github.Secret, 0) -// for key, _ := range m.secrets { -// secs = append(secs, &github.Secret{ -// Name: key, -// }) -// } - -// return &github.Secrets{ -// TotalCount: len(m.secrets), -// Secrets: secs, -// }, nil -// } - -// func (m MockSecretsService) DeleteRepoSecret(ctx context.Context, owner, repo, name string) error { -// delete(m.secrets, name) -// return nil -// } - -// func (m MockSecretsService) CreateOrUpdateRepoSecret(ctx context.Context, owner, repo string, eSecret *github.EncryptedSecret) error { -// m.secrets[eSecret.Name] = eSecret -// return nil -// } - -// func newMockSecretsService() poiana.ActionsSecretsService { -// mServ := &MockSecretsService{secrets: make(map[string]*github.EncryptedSecret, 0)} -// _ = mServ.CreateOrUpdateRepoSecret(context.Background(), "", "", &github.EncryptedSecret{ -// Name: "secret0", -// KeyID: "testing", -// }) -// return mServ -// } - -// func TestSyncServices(t *testing.T) { -// ctx := context.Background() -// secrets := []string{ -// "secret1", "secret2", -// } - -// mockServ := newMockSecretsService() -// provider, err := poiana.NewMockSecretsProvider(map[string]string{"secret1": "value1", "secret2": "value2"}) -// assert.NoError(t, err) - -// keyID := "testing" -// key := base64.StdEncoding.EncodeToString([]byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) // 32B key -// pKey := github.PublicKey{ -// KeyID: &keyID, -// Key: &key, -// } -// err = poiana.syncSecrets(ctx, mockServ, provider, &pKey, "", "", secrets) -// assert.NoError(t, err) - -// secs, err := mockServ.ListRepoSecrets(ctx, "", "") -// assert.NoError(t, err) - -// for _, sec := range secs.Secrets { -// assert.Contains(t, secrets, sec.Name) -// } -// } diff --git a/variables_test.go b/variables_test.go deleted file mode 100644 index 91ea6bc..0000000 --- a/variables_test.go +++ /dev/null @@ -1,69 +0,0 @@ -package main - -// import ( -// "context" -// "github.com/FedeDP/GhEnvSet/pkg/poiana" -// "github.com/google/go-github/v49/github" -// "github.com/stretchr/testify/assert" -// "testing" -// ) - -// type MockVariableService struct { -// variables map[string]string -// } - -// func (m MockVariableService) ListRepoVariables(ctx context.Context, owner, repo string) (*poiana.Variables, error) { -// vars := make([]*poiana.Variable, 0) -// for key, val := range m.variables { -// vars = append(vars, &poiana.Variable{ -// Name: key, -// Value: val, -// }) -// } - -// return &poiana.Variables{ -// TotalCount: len(m.variables), -// Variables: vars, -// }, nil -// } - -// func (m MockVariableService) DeleteRepoVariable(ctx context.Context, owner, repo, name string) error { -// delete(m.variables, name) -// return nil -// } - -// func (m MockVariableService) CreateOrUpdateRepoVariable(ctx context.Context, owner, repo string, variable *poiana.Variable) error { -// m.variables[variable.Name] = variable.Value -// return nil -// } - -// func newMockVariableService() poiana.ActionsVarsService { -// mServ := &MockVariableService{variables: make(map[string]string, 0)} -// // Initial variable set -// _ = mServ.CreateOrUpdateRepoVariable(context.Background(), "", "", &poiana.Variable{ -// Name: "test0", -// Value: "value0", -// CreatedAt: github.Timestamp{}, -// UpdatedAt: github.Timestamp{}, -// }) -// return mServ -// } - -// func TestSyncVariables(t *testing.T) { -// ctx := context.Background() -// variables := map[string]string{ -// "test1": "value1", -// "test2": "value2", -// } - -// mockServ := newMockVariableService() -// err := syncVariables(ctx, mockServ, "", "", variables) -// assert.NoError(t, err) - -// vars, err := mockServ.ListRepoVariables(ctx, "", "") -// assert.NoError(t, err) - -// for _, v := range vars.Variables { -// assert.Equal(t, variables[v.Name], v.Value) -// } -// }