Skip to content

Commit

Permalink
Added validation to ensure that a specific environment is selected wh…
Browse files Browse the repository at this point in the history
…en using local apply
  • Loading branch information
fritzduchardt committed Dec 17, 2023
1 parent 34ddc10 commit dd9dc70
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func init() {
log.Fatal().Err(err).Msg("Unable to initialize myks's globe")
}

Check warning on line 28 in cmd/apply.go

View check run for this annotation

Codecov / codecov/patch

cmd/apply.go#L26-L28

Added lines #L26 - L28 were not covered by tests

if !g.SingleEnv() {
log.Fatal().Msg("Local apply can only be used with a specific environment. Make sure you are connected to the right cluster.")
}

Check warning on line 32 in cmd/apply.go

View check run for this annotation

Codecov / codecov/patch

cmd/apply.go#L30-L32

Added lines #L30 - L32 were not covered by tests

if err := g.Apply(asyncLevel); err != nil {
log.Fatal().Err(err).Msg("Unable to apply k8s manifests")
}

Check warning on line 36 in cmd/apply.go

View check run for this annotation

Codecov / codecov/patch

cmd/apply.go#L34-L36

Added lines #L34 - L36 were not covered by tests
Expand Down
8 changes: 8 additions & 0 deletions internal/myks/globe.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,11 @@ func (g *Globe) Msg(msg string) string {
formattedMessage := fmt.Sprintf(GlobalLogFormat, msg)
return formattedMessage
}

func (g *Globe) SingleEnv() bool {
if g.environments == nil {
log.Error().Msg("Invoking SingleEnv() before Init()")
return false
}

Check warning on line 408 in internal/myks/globe.go

View check run for this annotation

Codecov / codecov/patch

internal/myks/globe.go#L406-L408

Added lines #L406 - L408 were not covered by tests
return len(g.environments) == 1
}
28 changes: 28 additions & 0 deletions internal/myks/globe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package myks

import "testing"

func TestGlobe_SingleEnv(t *testing.T) {
type fields struct {
environments map[string]*Environment
}
tests := []struct {
name string
fields fields
want bool
}{
{"before init", fields{map[string]*Environment{}}, false},
{"happy path", fields{map[string]*Environment{"test-env": {}}}, true},
{"sad path", fields{map[string]*Environment{"test-env": {}, "test-env2": {}}}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := &Globe{
environments: tt.fields.environments,
}
if got := g.SingleEnv(); got != tt.want {
t.Errorf("SingleEnv() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit dd9dc70

Please sign in to comment.