github.com/ddev/ddev@v1.23.2-0.20240519125000-d824ffe36ff3/pkg/config/state/storage/yaml/yaml_storage_test.go (about) 1 package yaml_test 2 3 import ( 4 "os" 5 "path/filepath" 6 "testing" 7 8 "github.com/ddev/ddev/pkg/config/state/storage/yaml" 9 "github.com/ddev/ddev/pkg/config/state/types" 10 "github.com/stretchr/testify/suite" 11 ) 12 13 func TestYamlStorageTestSuite(t *testing.T) { 14 suite.Run(t, new(YamlStorageTestSuite)) 15 } 16 17 type YamlStorageTestSuite struct { 18 suite.Suite 19 } 20 21 func (suite *YamlStorageTestSuite) TestReadProperlyLoadsState() { 22 var tests = []struct { 23 Key types.StateEntryKey 24 Value string 25 }{ 26 // key1 to 3 are available in the test data. 27 { 28 Key: "key1", 29 Value: "string1", 30 }, 31 { 32 Key: "key2", 33 Value: "string2", 34 }, 35 { 36 Key: "key3", 37 Value: "string3", 38 }, 39 } 40 41 storage := yaml.New(filepath.Join("testdata", "state.yml")) 42 data, err := storage.Read() 43 44 suite.NoError(err) 45 suite.NotNil(data) 46 47 for _, tt := range tests { 48 suite.Run(tt.Key, func() { 49 suite.EqualValues(tt.Value, data[tt.Key].(map[string]interface{})["value"]) 50 }) 51 } 52 } 53 54 func (suite *YamlStorageTestSuite) TestReadReturnsStateWithoutStateFile() { 55 storage := yaml.New(filepath.Join("testdata", "not_existing.yml")) 56 data, err := storage.Read() 57 58 suite.NoError(err) 59 suite.NotNil(data) 60 suite.EqualValues(types.RawState{}, data) 61 } 62 63 func (suite *YamlStorageTestSuite) TestReadReturnsErrorForInvalidStateFile() { 64 storage := yaml.New(filepath.Join("testdata", "invalid.yml")) 65 data, err := storage.Read() 66 67 suite.ErrorContains(err, "yaml: unmarshal errors") 68 suite.Nil(data) 69 } 70 71 func (suite *YamlStorageTestSuite) TestWriteProperlyWritesState() { 72 stateFile := filepath.Join(suite.T().TempDir(), "state.yml") 73 storage := yaml.New(stateFile) 74 75 // Simulation of the state.yml. 76 err := storage.Write(types.RawState{ 77 "key1": map[string]string{ 78 "value": "string1", 79 }, 80 "key2": map[string]string{ 81 "value": "string2", 82 }, 83 "key3": map[string]string{ 84 "value": "string3", 85 }, 86 }) 87 88 suite.NoError(err) 89 suite.FileExists(stateFile) 90 91 want, err := os.ReadFile(filepath.Join("testdata", "state.yml")) 92 if err != nil { 93 suite.FailNow("error reading state file:", err) 94 } 95 96 has, err := os.ReadFile(stateFile) 97 if err != nil { 98 suite.FailNow("error reading written state file:", err) 99 } 100 101 suite.Equal(want, has) 102 } 103 104 func BenchmarkRead(b *testing.B) { 105 storage := yaml.New(filepath.Join("testdata", "state.yml")) 106 b.ResetTimer() 107 108 for i := 0; i < b.N; i++ { 109 _, _ = storage.Read() 110 } 111 } 112 113 func BenchmarkWrite(b *testing.B) { 114 storage := yaml.New(filepath.Join(b.TempDir(), "state.yml")) 115 b.ResetTimer() 116 117 for i := 0; i < b.N; i++ { 118 _ = storage.Write(types.RawState{ 119 "key1": map[string]string{ 120 "value": "string1", 121 }, 122 "key2": map[string]string{ 123 "value": "string2", 124 }, 125 "key3": map[string]string{ 126 "value": "string3", 127 }, 128 }) 129 } 130 }