Skip to content

Commit

Permalink
new(tests): ported tests to new config.Loop API.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Jan 26, 2023
1 parent 64773f3 commit f88d563
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 175 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
Expand Down
152 changes: 152 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
30 changes: 11 additions & 19 deletions pkg/poiana/config.go → pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package poiana
package config

import (
"context"
"encoding/base64"
"io"
"github.com/FedeDP/GhEnvSet/pkg/poiana"
"os"
"strings"

Expand All @@ -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 {
Expand All @@ -48,16 +40,16 @@ 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
}
return &conf, nil
}

func syncSecrets(ctx context.Context,
service ActionsSecretsService,
provider SecretsProvider,
service poiana.ActionsSecretsService,
provider poiana.SecretsProvider,
pKey *github.PublicKey,
orgName, repoName string,
secrets []string) error {
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
})
Expand All @@ -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()
Expand Down
86 changes: 0 additions & 86 deletions secrets_test.go

This file was deleted.

Loading

0 comments on commit f88d563

Please sign in to comment.