Skip to content

Commit c222a70

Browse files
committed
wip unit tests pass
Signed-off-by: Carolyn Van Slyck <[email protected]>
1 parent d210125 commit c222a70

20 files changed

+66
-46
lines changed

pkg/encoding/array_map.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"gopkg.in/yaml.v3"
99
)
1010

11-
// TODO: Move this into its own package so that people can't muck with items
12-
1311
// MapElement is the in-memory representation of the item when stored in a map.
1412
type MapElement interface {
1513
ToArrayEntry(key string) ArrayElement
@@ -52,7 +50,26 @@ func (m *ArrayMap[T, K]) Items() map[string]T {
5250
return result
5351
}
5452

55-
func (m *ArrayMap[T, K]) UnsafeItems() map[string]T {
53+
func (m *ArrayMap[T, K]) ItemsSorted() []K {
54+
if m == nil {
55+
return nil
56+
}
57+
58+
result := make([]K, len(m.items))
59+
i := 0
60+
for k, v := range m.items {
61+
// I can't figure out how to constrain T such that ToArrayEntry returns K, so I'm doing a cast
62+
result[i] = v.ToArrayEntry(k).(K)
63+
i++
64+
}
65+
sort.SliceStable(result, func(i, j int) bool {
66+
return result[i].GetKey() < result[j].GetKey()
67+
})
68+
69+
return result
70+
}
71+
72+
func (m *ArrayMap[T, K]) ItemsUnsafe() map[string]T {
5673
if m == nil {
5774
return nil
5875
}
@@ -94,6 +111,9 @@ func (m *ArrayMap[T, K]) MarshalRaw() interface{} {
94111
}
95112

96113
var raw []ArrayElement
114+
if m.items == nil {
115+
return raw
116+
}
97117

98118
raw = make([]ArrayElement, 0, len(m.items))
99119
for k, v := range m.items {

pkg/encoding/array_map_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func TestArrayMap_Marshal(t *testing.T) {
7171
require.NoError(t, err, "ReadFile failed")
7272

7373
gotData, err := yaml.Marshal(m)
74+
require.NoError(t, err, "Marshal failed")
7475
assert.Equal(t, string(wantData), string(gotData))
7576
}
7677

@@ -86,7 +87,7 @@ func TestArrayMap_Unmarshal_DuplicateKeys(t *testing.T) {
8687
// check that when we round trip a null ArrayMap, it stays null and isn't initialized to an _empty_ ArrayMap
8788
// This impacts how it is marshaled later to yaml or json, because we often have fields tagged with omitempty
8889
// and so it must be null to not be written out.
89-
func TestArrayMap_RoundTrip_Null(t *testing.T) {
90+
func TestArrayMap_RoundTrip_Empty(t *testing.T) {
9091
wantData, err := os.ReadFile("testdata/array-empty.json")
9192
require.NoError(t, err, "ReadFile failed")
9293

pkg/generator/credentials_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestGoodCredentialsName(t *testing.T) {
5151

5252
cs, err := GenerateCredentials(opts)
5353
require.NoError(t, err, "name should NOT have resulted in an error")
54-
require.Equal(t, 3, cs.Credentials.Len(), "should have had 3 entries")
54+
require.Equal(t, 3, cs.Len(), "should have had 3 entries")
5555
}
5656

5757
func TestNoCredentials(t *testing.T) {

pkg/generator/generator_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import (
88
)
99

1010
func Test_genEmptySet(t *testing.T) {
11-
expected := secrets.ValueMapping{
12-
Source: secrets.Source{Hint: "TODO"},
13-
}
11+
expected := secrets.Source{Hint: "TODO"}
1412

1513
got, err := genEmptySet("emptyset", surveyParameters)
1614
require.NoError(t, err)
@@ -20,5 +18,5 @@ func Test_genEmptySet(t *testing.T) {
2018
func Test_genSurvey_unsupported(t *testing.T) {
2119
got, err := genSurvey("myturtleset", SurveyType("turtles"))
2220
require.EqualError(t, err, "unsupported survey type: turtles")
23-
require.Equal(t, secrets.ValueMapping{}, got)
21+
require.Equal(t, secrets.Source{}, got)
2422
}

pkg/porter/credentials.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ func (p *Porter) ShowCredential(ctx context.Context, opts CredentialShowOptions)
272272
var rows [][]string
273273

274274
// Iterate through all CredentialStrategies and add to rows
275-
for csName, cs := range credSet.Iterate() {
276-
rows = append(rows, []string{csName, cs.Source.Hint, cs.Source.Strategy})
275+
for _, cs := range credSet.IterateSorted() {
276+
rows = append(rows, []string{cs.Name, cs.Source.Hint, cs.Source.Strategy})
277277
}
278278

279279
// Build and configure our tablewriter

pkg/porter/credentials_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ func TestPorter_CredentialsApply(t *testing.T) {
564564
require.NoError(t, err, "Failed to retrieve applied credential set")
565565

566566
assert.Equal(t, "kool-kreds", cs.Name, "unexpected credential set name")
567-
require.Len(t, cs.Credentials, 4, "expected 4 credentials in the set")
567+
require.Equal(t, 4, cs.Len(), "expected 4 credentials in the set")
568568
koolCred, ok := cs.Get("kool-config")
569569
require.True(t, ok, "expected 'kool-config' to be present")
570570
assert.Equal(t, "path", koolCred.Source.Strategy, "unexpected credential source")

pkg/porter/helpers.go

+1
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ func (p *TestPorter) AddTestBundleDir(bundleDir string, generateUniqueName bool)
234234
// When they are different and PORTER_UPDATE_TEST_FILES is true, the file is updated to match
235235
// the new test output.
236236
func (p *TestPorter) CompareGoldenFile(goldenFile string, got string) {
237+
p.T().Helper()
237238
p.TestConfig.TestContext.CompareGoldenFile(goldenFile, got)
238239
}
239240

pkg/porter/install_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func TestPorter_ApplyParametersToInstallation(t *testing.T) {
8181

8282
newPS := storage.NewParameterSet("", "newps1")
8383
newPS.SetStrategy("logLevel", secrets.HardCodedValueStrategy("11"))
84+
p.TestParameters.InsertParameterSet(ctx, newPS)
8485

8586
inst := storage.NewInstallation("myns", "mybuns")
8687
inst.Bundle = storage.OCIReferenceParts{

pkg/porter/lifecycle_test.go

+7-18
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ func TestPorter_applyActionOptionsToInstallation_sanitizesParameters(t *testing.
448448

449449
err = p.applyActionOptionsToInstallation(ctx, opts, &i)
450450
require.NoError(t, err)
451-
require.Len(t, i.Parameters.Parameters, 2)
451+
require.Equal(t, 2, i.Parameters.Len())
452452

453453
// there should be no sensitive value on installation record
454454
for paramName, param := range i.Parameters.Iterate() {
@@ -474,11 +474,9 @@ func TestPorter_applyActionOptionsToInstallation_sanitizesParameters(t *testing.
474474
// Check that when no parameter overrides are specified, we use the originally specified parameters from the previous run
475475
wantParameters := &storage.ParameterSourceMap{}
476476
wantParameters.Set("my-first-param", storage.ParameterSource{
477-
Source: secrets.Source{Strategy: "value", Hint: "1"},
478-
ResolvedValue: "1"})
477+
Source: secrets.Source{Strategy: "value", Hint: "1"}})
479478
wantParameters.Set("my-second-param", storage.ParameterSource{
480-
Source: secrets.Source{Strategy: "secret", Hint: "INSTALLATIONID-my-second-param"},
481-
ResolvedValue: "2"})
479+
Source: secrets.Source{Strategy: "secret", Hint: "INSTALLATIONID-my-second-param"}})
482480
require.Equal(t, wantParameters, i.Parameters.Parameters)
483481
}
484482

@@ -518,23 +516,14 @@ func TestPorter_applyActionOptionsToInstallation_PreservesExistingParams(t *test
518516

519517
err = p.applyActionOptionsToInstallation(ctx, opts, &i)
520518
require.NoError(t, err)
521-
require.Len(t, i.Parameters.Parameters, 2)
519+
require.Equal(t, 2, i.Parameters.Len())
522520

523521
// Check that overrides are applied on top of existing parameters
524522
wantParameters := &storage.ParameterSourceMap{}
525523
wantParameters.Set("my-first-param", storage.ParameterSource{
526-
Source: secrets.Source{Strategy: "value", Hint: "3"},
527-
ResolvedValue: "3"})
524+
Source: secrets.Source{Strategy: "value", Hint: "3"},
525+
})
528526
wantParameters.Set("my-second-param", storage.ParameterSource{
529-
Source: secrets.Source{Strategy: "secret", Hint: "INSTALLATIONID-my-second-param"},
530-
ResolvedValue: "2"})
527+
Source: secrets.Source{Strategy: "secret", Hint: "INSTALLATIONID-my-second-param"}})
531528
require.Equal(t, wantParameters, i.Parameters.Parameters)
532-
533-
// Check the values stored are correct
534-
params, err := p.Parameters.ResolveAll(ctx, i.Parameters)
535-
require.NoError(t, err, "Failed to resolve the installation parameters")
536-
require.Equal(t, secrets.Set{
537-
"my-first-param": "3", // Should have used the override
538-
"my-second-param": "2", // Should have kept the existing value from the last run
539-
}, params, "Incorrect parameter values were persisted on the installationß")
540529
}

pkg/porter/show_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ func TestPorter_ShowInstallationWithBundle(t *testing.T) {
108108
r.BundleDigest = "sha256:88d68ef0bdb9cedc6da3a8e341a33e5d2f8bb19d0cf7ec3f1060d3f9eb73cae9"
109109

110110
r.ParameterOverrides = i.NewInternalParameterSet()
111-
r.ParameterOverrides.SetStrategy("logLevel", secrets.HardCodedValueStrategy("3"))
112-
r.ParameterOverrides.SetStrategy("secretString", secrets.HardCodedValueStrategy("foo"))
111+
r.ParameterOverrides.Set("logLevel", secrets.HardCodedValue("3"))
112+
r.ParameterOverrides.Set("secretString", secrets.HardCodedValue("foo"))
113113

114114
r.Parameters = i.NewInternalParameterSet()
115-
r.ParameterOverrides.SetStrategy("logLevel", secrets.HardCodedValueStrategy("3"))
116-
r.ParameterOverrides.SetStrategy("token", secrets.HardCodedValueStrategy("top-secret"))
117-
r.ParameterOverrides.SetStrategy("secretString", secrets.HardCodedValueStrategy("foo"))
115+
r.Parameters.Set("logLevel", secrets.HardCodedValue("3"))
116+
r.Parameters.Set("token", secrets.HardCodedValue("top-secret"))
117+
r.Parameters.Set("secretString", secrets.HardCodedValue("foo"))
118118

119119
r.ParameterSets = []string{"dev-env"}
120120
r.ParameterOverrides.Parameters = p.SanitizeParameters(r.ParameterOverrides.Parameters, r.ID, bun)

pkg/portercontext/helpers.go

+1
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ func (c *TestContext) hasChild(dir string, childName string) (string, bool) {
314314
// When they are different and PORTER_UPDATE_TEST_FILES is true, the file is updated to match
315315
// the new test output.
316316
func (c *TestContext) CompareGoldenFile(goldenFile string, got string) {
317+
c.T.Helper()
317318
test.CompareGoldenFile(c.T, goldenFile, got)
318319
}
319320

pkg/secrets/map.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func (m Map) Merge(overrides Map) Map {
1313

1414
func (m Map) ToResolvedValues() map[string]string {
1515
values := make(map[string]string, m.Len())
16-
for k, v := range m.UnsafeItems() {
16+
for k, v := range m.ItemsUnsafe() {
1717
values[k] = v.ResolvedValue
1818
}
1919
return values

pkg/storage/credential_store_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestCredentialStorage_CRUD(t *testing.T) {
4141
require.NoError(t, cp.UpdateCredentialSet(context.Background(), cs))
4242
cs, err = cp.GetCredentialSet(context.Background(), cs.Namespace, cs.Name)
4343
require.NoError(t, err)
44-
assert.Len(t, cs.Credentials, 2)
44+
assert.Equal(t, 2, cs.Len())
4545

4646
cs2 := NewCredentialSet("dev", "sekrets-2")
4747
cs2.SetStrategy("password-2", secrets.Source{

pkg/storage/credentialset.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package storage
33
import (
44
"context"
55
"fmt"
6-
"get.porter.sh/porter/pkg/encoding"
76
"strings"
87
"time"
98

109
"get.porter.sh/porter/pkg/cnab"
10+
"get.porter.sh/porter/pkg/encoding"
1111
"get.porter.sh/porter/pkg/schema"
1212
"get.porter.sh/porter/pkg/secrets"
1313
"get.porter.sh/porter/pkg/tracing"
@@ -136,6 +136,10 @@ func (s CredentialSet) Iterate() map[string]CredentialSource {
136136
return s.Credentials.Items()
137137
}
138138

139+
func (s CredentialSet) IterateSorted() []NamedCredentialSource {
140+
return s.Credentials.ItemsSorted()
141+
}
142+
139143
func (s CredentialSet) SetStrategy(key string, source secrets.Source) {
140144
s.Credentials.Set(key, CredentialSource{Source: source})
141145
}
@@ -151,6 +155,10 @@ func (s CredentialSet) Get(key string) (CredentialSource, bool) {
151155
return s.Credentials.Get(key)
152156
}
153157

158+
func (s CredentialSet) Len() int {
159+
return s.Credentials.Len()
160+
}
161+
154162
// Validate compares the given credentials with the spec.
155163
//
156164
// This will result in an error only when the following conditions are true:

pkg/storage/credentialset_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestNewCredentialSet(t *testing.T) {
3030
assert.Equal(t, cs.Status.Created, cs.Status.Modified, "Created and Modified should have the same timestamp")
3131
assert.Equal(t, SchemaTypeCredentialSet, cs.SchemaType, "incorrect SchemaType")
3232
assert.Equal(t, DefaultCredentialSetSchemaVersion, cs.SchemaVersion, "incorrect SchemaVersion")
33-
assert.Len(t, cs.Credentials, 1, "Credentials should be initialized with 1 value")
33+
assert.Equal(t, 1, cs.Len(), "Credentials should be initialized with 1 value")
3434
}
3535

3636
func TestValidate(t *testing.T) {

pkg/storage/migrations/migration_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func validateMigratedCredentialSets(ctx context.Context, t *testing.T, destStore
236236
assert.Equal(t, "2022-06-06T16:06:52.099455-05:00", creds.Status.Created.Format(time.RFC3339Nano), "incorrect created timestamp")
237237
assert.Equal(t, "2022-06-06T16:07:52.099455-05:00", creds.Status.Modified.Format(time.RFC3339Nano), "incorrect modified timestamp")
238238
assert.Empty(t, creds.Labels, "incorrect labels")
239-
require.Equal(t, 1, creds.Credentials.Len(), "incorrect number of credentials migrated")
239+
require.Equal(t, 1, creds.Len(), "incorrect number of credentials migrated")
240240

241241
cred, ok := creds.Get("github-token")
242242
require.True(t, ok, "expected 'github-token' to be present")
@@ -260,7 +260,7 @@ func validateMigratedParameterSets(ctx context.Context, t *testing.T, destStore
260260
assert.Equal(t, "2022-06-06T16:06:21.635528-05:00", ps.Status.Created.Format(time.RFC3339Nano), "incorrect created timestamp")
261261
assert.Equal(t, "2022-06-06T17:06:21.635528-05:00", ps.Status.Modified.Format(time.RFC3339Nano), "incorrect modified timestamp")
262262
assert.Empty(t, ps.Labels, "incorrect labels")
263-
require.Equal(t, 1, ps.Parameters.Len(), "incorrect number of parameters migrated")
263+
require.Equal(t, 1, ps.Len(), "incorrect number of parameters migrated")
264264

265265
param, ok := ps.Get("name")
266266
require.True(t, ok, "expected 'name' parameter to be present")

pkg/storage/parameterset_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestNewParameterSet(t *testing.T) {
2828
assert.Equal(t, ps.Status.Created, ps.Status.Modified, "Created and Modified should have the same timestamp")
2929
assert.Equal(t, SchemaTypeParameterSet, ps.SchemaType, "incorrect SchemaType")
3030
assert.Equal(t, DefaultParameterSetSchemaVersion, ps.SchemaVersion, "incorrect SchemaVersion")
31-
assert.Len(t, ps.Parameters, 1, "Parameters should be initialized with 1 value")
31+
assert.Equal(t, 1, ps.Len(), "Parameters should be initialized with 1 value")
3232
}
3333

3434
func TestParameterSet_String(t *testing.T) {

pkg/storage/run_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ func TestRun_TypedParameterValues(t *testing.T) {
172172
run := NewRun("dev", "mybuns")
173173
run.Bundle = bun
174174
run.Parameters = NewParameterSet(run.Namespace, run.Bundle.Name)
175-
run.Parameters.SetStrategy("baz", secrets.HardCodedValueStrategy("baz-test"))
176-
run.Parameters.SetStrategy("name", secrets.HardCodedValueStrategy("porter-test"))
177-
run.Parameters.SetStrategy("porter-state", secrets.HardCodedValueStrategy(""))
175+
run.Parameters.Set("baz", secrets.HardCodedValue("baz-test"))
176+
run.Parameters.Set("name", secrets.HardCodedValue("porter-test"))
177+
run.Parameters.Set("porter-state", secrets.HardCodedValue(""))
178178
run.Parameters.Set("foo", ParameterSource{
179179
Source: secrets.Source{Strategy: "secret", Hint: "runID"},
180180
ResolvedValue: "5"})
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"schemaVersion":"","_id":"foo","created":"0001-01-01T00:00:00Z","namespace":"","installation":"","revision":"","action":"","bundleReference":"","bundleDigest":"","parameterOverrides":{"schemaVersion":"","namespace":"","name":"","parameters":[],"status":{"created":"0001-01-01T00:00:00Z","modified":"0001-01-01T00:00:00Z"}},"parameters":{"schemaVersion":"","namespace":"","name":"","parameters":[],"status":{"created":"0001-01-01T00:00:00Z","modified":"0001-01-01T00:00:00Z"}},"custom":null,"bundle":"{\"actions\":{\"logs\":{},\"test\":{\"modifies\":true}},\"description\":\"this is my bundle\",\"invocationImages\":[],\"name\":\"mybun\",\"schemaVersion\":\"schemaVersion\",\"version\":\"v0.1.0\"}"}
1+
{"schemaVersion":"","_id":"foo","created":"0001-01-01T00:00:00Z","namespace":"","installation":"","revision":"","action":"","bundleReference":"","bundleDigest":"","parameterOverrides":{"schemaVersion":"","namespace":"","name":"","parameters":null,"status":{"created":"0001-01-01T00:00:00Z","modified":"0001-01-01T00:00:00Z"}},"parameters":{"schemaVersion":"","namespace":"","name":"","parameters":null,"status":{"created":"0001-01-01T00:00:00Z","modified":"0001-01-01T00:00:00Z"}},"custom":null,"bundle":"{\"actions\":{\"logs\":{},\"test\":{\"modifies\":true}},\"description\":\"this is my bundle\",\"invocationImages\":[],\"name\":\"mybun\",\"schemaVersion\":\"schemaVersion\",\"version\":\"v0.1.0\"}"}

pkg/test/helper.go

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func TestMainWithMockedCommandHandlers(m *testing.M) {
6969
// When they are different and PORTER_UPDATE_TEST_FILES is true, the file is updated to match
7070
// the new test output.
7171
func CompareGoldenFile(t *testing.T, goldenFile string, got string) {
72+
t.Helper()
7273
if os.Getenv("PORTER_UPDATE_TEST_FILES") == "true" {
7374
os.MkdirAll(filepath.Dir(goldenFile), pkg.FileModeDirectory)
7475
t.Logf("Updated test file %s to match latest test output", goldenFile)

0 commit comments

Comments
 (0)