| 10 | ) |
| 11 | |
| 12 | func TestSecretdYAML(t *testing.T) { |
| 13 | // Test embedding of Secret. |
| 14 | { |
| 15 | type TestStruct struct { |
| 16 | Secret Secret `yaml:"secret"` |
| 17 | } |
| 18 | |
| 19 | var testStruct TestStruct |
| 20 | require.NoError(t, testStruct.Secret.Set("pa55w0rd")) |
| 21 | expected := []byte(`secret: '********' |
| 22 | `) |
| 23 | |
| 24 | actual, err := yaml.Marshal(testStruct) |
| 25 | require.NoError(t, err) |
| 26 | assert.Equal(t, expected, actual) |
| 27 | |
| 28 | var actualStruct TestStruct |
| 29 | yamlSecret := []byte(`secret: pa55w0rd |
| 30 | `) |
| 31 | err = yaml.Unmarshal(yamlSecret, &actualStruct) |
| 32 | require.NoError(t, err) |
| 33 | assert.Equal(t, testStruct, actualStruct) |
| 34 | } |
| 35 | |
| 36 | // Test pointers of Secret. |
| 37 | { |
| 38 | type TestStruct struct { |
| 39 | Secret *Secret `yaml:"secret"` |
| 40 | } |
| 41 | |
| 42 | var testStruct TestStruct |
| 43 | testStruct.Secret = &Secret{} |
| 44 | require.NoError(t, testStruct.Secret.Set("pa55w0rd")) |
| 45 | expected := []byte(`secret: '********' |
| 46 | `) |
| 47 | |
| 48 | actual, err := yaml.Marshal(testStruct) |
| 49 | require.NoError(t, err) |
| 50 | assert.Equal(t, expected, actual) |
| 51 | |
| 52 | var actualStruct TestStruct |
| 53 | yamlSecret := []byte(`secret: pa55w0rd |
| 54 | `) |
| 55 | err = yaml.Unmarshal(yamlSecret, &actualStruct) |
| 56 | require.NoError(t, err) |
| 57 | assert.Equal(t, testStruct, actualStruct) |
| 58 | } |
| 59 | |
| 60 | // Test no value set in Secret. |
| 61 | { |
| 62 | type TestStruct struct { |
| 63 | Secret Secret `yaml:"secret"` |
| 64 | } |
| 65 | var testStruct TestStruct |
| 66 | expected := []byte(`secret: "" |
| 67 | `) |
| 68 | |
| 69 | actual, err := yaml.Marshal(testStruct) |