Best Testkube code snippet using config.TestStorage
manager_test.go
Source:manager_test.go
1package rotate2import (3 "context"4 "fmt"5 "testing"6 "time"7 "github.com/stretchr/testify/assert"8 "github.com/stretchr/testify/require"9 "github.com/zostay/garotate/pkg/config"10 "github.com/zostay/garotate/pkg/plugin"11 "github.com/zostay/garotate/pkg/secret"12)13var (14 pastDate = time.Date(15 2022, time.April, 1,16 0, 0, 0, 0,17 time.UTC,18 )19 recentButPastDate = time.Date(20 time.Now().Year(), time.Now().Month(), time.Now().Day(),21 time.Now().Hour()-12, 0, 0, 0,22 time.UTC,23 )24 futureDate = time.Date(25 time.Now().Year()+1, time.April, 1,26 0, 0, 0, 0,27 time.UTC,28 )29)30type testClientSecret struct {31 call string32 sec secret.Info33}34type testClient struct {35 lastCallSecrets []testClientSecret36 lastRotated time.Time37 failLastRotated int38 failRotateSecret int39}40func NewTestClient() *testClient {41 return &testClient{42 lastCallSecrets: []testClientSecret{},43 lastRotated: pastDate,44 failLastRotated: -1,45 failRotateSecret: -1,46 }47}48func (c *testClient) Name() string {49 return "test"50}51func (c *testClient) Keys() secret.Map {52 return secret.Map{53 "alpha": "",54 "beta": "",55 }56}57func (c *testClient) LastRotated(ctx context.Context, s secret.Info) (time.Time, error) {58 c.lastCallSecrets = append(c.lastCallSecrets, testClientSecret{59 call: "LastRotated",60 sec: s,61 })62 if c.failLastRotated == 0 {63 return time.Time{}, fmt.Errorf("last updated bad stuff")64 } else {65 c.failLastRotated--66 return c.lastRotated, nil67 }68}69func (c *testClient) RotateSecret(70 ctx context.Context,71 s secret.Info,72) (secret.Map, error) {73 c.lastCallSecrets = append(c.lastCallSecrets, testClientSecret{74 call: "RotateSecret",75 sec: s,76 })77 if c.failRotateSecret == 0 {78 return nil, fmt.Errorf("rotate bad stuff")79 } else {80 c.failRotateSecret--81 return secret.Map{82 "alpha": "one",83 "beta": "two",84 }, nil85 }86}87func TestHappyManagerDryRun(t *testing.T) {88 pluginMgr := plugin.NewManager(89 config.PluginList{},90 )91 c := NewTestClient()92 c.failRotateSecret = 093 m := New(c, 0, true,94 pluginMgr,95 []config.Secret{96 {SecretName: "James"},97 {SecretName: "John"},98 },99 )100 ctx := context.Background()101 err := m.RotateSecrets(ctx)102 assert.NoError(t, err, "no error on rotate secrets dry run")103 callSecrets := []testClientSecret{104 {call: "LastRotated", sec: &config.Secret{SecretName: "James"}},105 {call: "LastRotated", sec: &config.Secret{SecretName: "John"}},106 }107 assert.Equal(t, callSecrets, c.lastCallSecrets, "only two calls made")108}109func TestSadManagerDryRun(t *testing.T) {110 pluginMgr := plugin.NewManager(111 config.PluginList{},112 )113 c := NewTestClient()114 c.failLastRotated = 0115 m := New(c, 0, true,116 pluginMgr,117 []config.Secret{118 {SecretName: "Andrew"},119 {SecretName: "Peter"},120 },121 )122 ctx := context.Background()123 err := m.RotateSecrets(ctx)124 assert.NoError(t, err, "no error on rotate secretsd dry run even with errors")125 callSecrets := []testClientSecret{126 {call: "LastRotated", sec: &config.Secret{SecretName: "Andrew"}},127 {call: "LastRotated", sec: &config.Secret{SecretName: "Peter"}},128 }129 assert.Equal(t, callSecrets, c.lastCallSecrets, "only two calls made")130}131func TestHappyManager(t *testing.T) {132 pluginMgr := plugin.NewManager(133 config.PluginList{},134 )135 c := NewTestClient()136 m := New(c, 0, false,137 pluginMgr,138 []config.Secret{139 {SecretName: "Philip"},140 {SecretName: "Bartholomew"},141 },142 )143 ctx := context.Background()144 err := m.RotateSecrets(ctx)145 assert.NoError(t, err, "no error on rotate secrets happy run")146 callSecrets := []testClientSecret{147 {call: "LastRotated", sec: &config.Secret{SecretName: "Philip"}},148 {call: "RotateSecret", sec: &config.Secret{SecretName: "Philip"}},149 {call: "LastRotated", sec: &config.Secret{SecretName: "Bartholomew"}},150 {call: "RotateSecret", sec: &config.Secret{SecretName: "Bartholomew"}},151 }152 assert.Equal(t, callSecrets, c.lastCallSecrets, "all four calls made on happy run")153}154func TestSadManagerFailToRotate(t *testing.T) {155 pluginMgr := plugin.NewManager(156 config.PluginList{},157 )158 c := NewTestClient()159 c.failRotateSecret = 0160 m := New(c, 0, false,161 pluginMgr,162 []config.Secret{163 {SecretName: "Philip"},164 {SecretName: "Bartholomew"},165 },166 )167 ctx := context.Background()168 err := m.RotateSecrets(ctx)169 assert.Error(t, err, "got errors during sad rotation")170 callSecrets := []testClientSecret{171 {call: "LastRotated", sec: &config.Secret{SecretName: "Philip"}},172 {call: "RotateSecret", sec: &config.Secret{SecretName: "Philip"}},173 {call: "LastRotated", sec: &config.Secret{SecretName: "Bartholomew"}},174 {call: "RotateSecret", sec: &config.Secret{SecretName: "Bartholomew"}},175 }176 assert.Equal(t, callSecrets, c.lastCallSecrets, "all four calls made even when sad")177}178type testStorage struct {179 storage map[string]map[string]string180 lastSaved time.Time181 failLastSaved int182 failSaveKeys int183}184func (t *testStorage) Name() string {185 return "test storage"186}187func (t *testStorage) testStorage(store secret.Storage) map[string]string {188 if t.storage == nil {189 t.storage = make(map[string]map[string]string)190 }191 if _, ok := t.storage[store.Name()]; !ok {192 t.storage[store.Name()] = make(map[string]string)193 }194 return t.storage[store.Name()]195}196func (t *testStorage) LastSaved(197 ctx context.Context,198 store secret.Storage,199 key string,200) (time.Time, error) {201 if t.failLastSaved == 0 {202 return time.Time{}, fmt.Errorf("last saved bad stuff")203 } else {204 t.failLastSaved--205 }206 ts := t.testStorage(store)207 if _, found := ts[key]; !found {208 return time.Time{}, secret.ErrKeyNotFound209 }210 return t.lastSaved, nil211}212func (t *testStorage) SaveKeys(213 ctx context.Context,214 store secret.Storage,215 ss secret.Map,216) error {217 if t.failSaveKeys == 0 {218 return fmt.Errorf("save keys bad stuff")219 } else {220 t.failSaveKeys--221 }222 ts := t.testStorage(store)223 for k, v := range ss {224 ts[k] = v225 }226 return nil227}228type testRotationBuilder struct{}229func (b *testRotationBuilder) Build(230 ctx context.Context,231 c *config.Plugin,232) (plugin.Instance, error) {233 return NewTestClient(), nil234}235type testStorageBuilder struct{}236func (b *testStorageBuilder) Build(237 ctx context.Context,238 c *config.Plugin,239) (plugin.Instance, error) {240 failLastSaved := -1241 if fls, ok := c.Options["failLastSaved"]; ok {242 flsi, ok := fls.(int)243 if !ok {244 panic("test configuration is wrong in the failLastSaved key")245 }246 failLastSaved = flsi247 }248 failSaveKeys := -1249 if fsk, ok := c.Options["failSaveKeys"]; ok {250 fski, ok := fsk.(int)251 if !ok {252 panic("test configuration is wrong in the failSaveKeys key")253 }254 failSaveKeys = fski255 }256 return &testStorage{257 lastSaved: futureDate,258 failLastSaved: failLastSaved,259 failSaveKeys: failSaveKeys,260 }, nil261}262func init() {263 plugin.Register("testStorage", new(testStorageBuilder))264 plugin.Register("testRotation", new(testRotationBuilder))265}266func TestHappyRotationStorage(t *testing.T) {267 pluginMgr := plugin.NewManager(268 config.PluginList{269 "test": config.Plugin{270 Name: "test",271 Package: "testStorage",272 },273 },274 )275 tstoreSettings := []testStorage{276 {277 storage: nil,278 lastSaved: futureDate,279 },280 {281 storage: map[string]map[string]string{282 "Matthew": map[string]string{283 "omega": "hunter2",284 "beta": "hunter",285 },286 },287 lastSaved: pastDate,288 },289 }290 for _, tss := range tstoreSettings {291 c := NewTestClient()292 c.lastRotated = recentButPastDate293 m := New(c, 24*time.Hour, false,294 pluginMgr,295 []config.Secret{296 {297 SecretName: "Matthew",298 Storages: []config.StorageMap{299 {300 StorageClient: "test",301 StorageName: "Matthew",302 Keys: config.KeyMap{303 "alpha": "omega",304 },305 },306 },307 },308 },309 )310 // cheating: we trigger the lazy construction here so we can manipulate311 // the state of the test object. This is highly dependent on how plugin312 // instance caching works.313 ctx := context.Background()314 store, err := pluginMgr.Instance(ctx, "test")315 assert.NoError(t, err, "got no errors retrieving storage instance")316 tstore, ok := store.(*testStorage)317 require.True(t, ok, "type coercion to testStorage works")318 // ensure we have a clean store before rotation319 tstore.storage = tss.storage320 tstore.lastSaved = tss.lastSaved321 err = m.RotateSecrets(ctx)322 assert.NoError(t, err, "got no errors during rotation")323 assert.Equal(t, tstore.storage,324 map[string]map[string]string{325 "Matthew": map[string]string{326 "omega": "one",327 "beta": "two",328 },329 },330 "expected keys found in store",331 )332 }333}334func TestHappyRotationStorageDryRun(t *testing.T) {335 pluginMgr := plugin.NewManager(336 config.PluginList{337 "test": config.Plugin{338 Name: "test",339 Package: "testStorage",340 },341 },342 )343 tstoreSettings := []testStorage{344 {345 storage: map[string]map[string]string{346 "Matthew": nil,347 },348 lastSaved: futureDate,349 },350 {351 storage: map[string]map[string]string{352 "Matthew": map[string]string{353 "omega": "hunter2",354 "beta": "hunter",355 },356 },357 lastSaved: pastDate,358 },359 }360 for _, tss := range tstoreSettings {361 c := NewTestClient()362 c.lastRotated = recentButPastDate363 m := New(c, 24*time.Hour, true,364 pluginMgr,365 []config.Secret{366 {367 SecretName: "Matthew",368 Storages: []config.StorageMap{369 {370 StorageClient: "test",371 StorageName: "Matthew",372 Keys: config.KeyMap{373 "alpha": "omega",374 },375 },376 },377 },378 },379 )380 // cheating: we trigger the lazy construction here so we can manipulate381 // the state of the test object. This is highly dependent on how plugin382 // instance caching works.383 ctx := context.Background()384 store, err := pluginMgr.Instance(ctx, "test")385 assert.NoError(t, err, "got no errors retrieving storage instance")386 tstore, ok := store.(*testStorage)387 require.True(t, ok, "type coercion to testStorage works")388 // ensure we have a clean store before rotation389 tstore.storage = tss.storage390 tstore.lastSaved = tss.lastSaved391 err = m.RotateSecrets(ctx)392 assert.NoError(t, err, "got no errors during rotation")393 assert.Equal(t, tss.storage, tstore.storage, "dry run changes nothing")394 }395}396func TestHappyRotationStorageSkipping(t *testing.T) {397 pluginMgr := plugin.NewManager(398 config.PluginList{399 "test": config.Plugin{400 Name: "test",401 Package: "testStorage",402 },403 },404 )405 fixtures := []struct {406 moniker string407 store testStorage408 }{409 {410 moniker: "with existing storage",411 store: testStorage{412 storage: map[string]map[string]string{413 "Matthew": map[string]string{414 "omega": "hunter2",415 "beta": "hunter",416 },417 },418 lastSaved: futureDate,419 },420 },421 }422 for _, fixture := range fixtures {423 tss := fixture.store424 c := NewTestClient()425 c.lastRotated = recentButPastDate426 m := New(c, 24*time.Hour, false,427 pluginMgr,428 []config.Secret{429 {430 SecretName: "Matthew",431 Storages: []config.StorageMap{432 {433 StorageClient: "test",434 StorageName: "Matthew",435 Keys: config.KeyMap{436 "alpha": "omega",437 },438 },439 },440 },441 },442 )443 // cheating: we trigger the lazy construction here so we can manipulate444 // the state of the test object. This is highly dependent on how plugin445 // instance caching works.446 ctx := context.Background()447 store, err := pluginMgr.Instance(ctx, "test")448 assert.NoErrorf(t, err,449 "got no errors retrieving storage instance [%s]", fixture.moniker)450 tstore, ok := store.(*testStorage)451 require.Truef(t, ok, "type coercion to testStorage works [%s]",452 fixture.moniker)453 // ensure we have a clean store before rotation454 tstore.storage = tss.storage455 tstore.lastSaved = tss.lastSaved456 err = m.RotateSecrets(ctx)457 assert.NoErrorf(t, err, "got no errors during rotation [%s]",458 fixture.moniker)459 assert.Equalf(t, tss.storage, tstore.storage,460 "keys in storage are unchanged [%s]", fixture.moniker)461 }462}463func TestSadRotationMissingStorage(t *testing.T) {464 pluginMgr := plugin.NewManager(465 config.PluginList{},466 )467 c := NewTestClient()468 c.lastRotated = recentButPastDate469 m := New(c, 24*time.Hour, false,470 pluginMgr,471 []config.Secret{472 {473 SecretName: "Thomas",474 Storages: []config.StorageMap{475 {476 StorageClient: "test",477 StorageName: "Thomas",478 Keys: config.KeyMap{479 "alpha": "omega",480 },481 },482 },483 },484 },485 )486 // cheating: we trigger the lazy construction here so we can manipulate487 // the state of the test object. This is highly dependent on how plugin488 // instance caching works.489 ctx := context.Background()490 err := m.RotateSecrets(ctx)491 // TODO It would be nice to test for log messages.492 assert.NoError(t, err, "error occurred, but only logged")493}494func TestSadRotationBrokenStorage(t *testing.T) {495 pluginMgr := plugin.NewManager(496 config.PluginList{497 "test": config.Plugin{498 Name: "test",499 Package: "testStorage",500 Options: map[string]any{501 "failLastSaved": 0,502 },503 },504 },505 )506 c := NewTestClient()507 c.lastRotated = recentButPastDate508 m := New(c, 24*time.Hour, false,509 pluginMgr,510 []config.Secret{511 {512 SecretName: "Thomas",513 Storages: []config.StorageMap{514 {515 StorageClient: "test",516 StorageName: "Thomas",517 Keys: config.KeyMap{518 "alpha": "omega",519 },520 },521 },522 },523 },524 )525 // cheating: we trigger the lazy construction here so we can manipulate526 // the state of the test object. This is highly dependent on how plugin527 // instance caching works.528 ctx := context.Background()529 err := m.RotateSecrets(ctx)530 // TODO It would be nice to test for log messages.531 assert.NoError(t, err, "error occurred, but only logged")532}533func TestSadRotationStorageWrongType(t *testing.T) {534 pluginMgr := plugin.NewManager(535 config.PluginList{536 "test": config.Plugin{537 Name: "test",538 Package: "testRotation",539 },540 },541 )542 c := NewTestClient()543 c.lastRotated = recentButPastDate544 m := New(c, 24*time.Hour, false,545 pluginMgr,546 []config.Secret{547 {548 SecretName: "Thomas",549 Storages: []config.StorageMap{550 {551 StorageClient: "test",552 StorageName: "Thomas",553 Keys: config.KeyMap{554 "alpha": "omega",555 },556 },557 },558 },559 },560 )561 // cheating: we trigger the lazy construction here so we can manipulate562 // the state of the test object. This is highly dependent on how plugin563 // instance caching works.564 ctx := context.Background()565 err := m.RotateSecrets(ctx)566 // TODO It would be nice to test for log messages.567 assert.NoError(t, err, "error occurred, but only logged")568}569func TestSadRotationStorageBrokenSaveKeys(t *testing.T) {570 pluginMgr := plugin.NewManager(571 config.PluginList{572 "test": config.Plugin{573 Name: "test",574 Package: "testStorage",575 Options: map[string]any{576 "failLastSaved": -1,577 "failSaveKeys": 0,578 },579 },580 },581 )582 c := NewTestClient()583 c.lastRotated = recentButPastDate584 m := New(c, 24*time.Hour, false,585 pluginMgr,586 []config.Secret{587 {588 SecretName: "Thomas",589 Storages: []config.StorageMap{590 {591 StorageClient: "test",592 StorageName: "Thomas",593 Keys: config.KeyMap{594 "alpha": "omega",595 },596 },597 },598 },599 },600 )601 // cheating: we trigger the lazy construction here so we can manipulate602 // the state of the test object. This is highly dependent on how plugin603 // instance caching works.604 ctx := context.Background()605 err := m.RotateSecrets(ctx)606 // TODO It would be nice to test for log messages.607 assert.Error(t, err, "expected error occurred")608}...
extension.go
Source:extension.go
...18 "go.opentelemetry.io/collector/config"19 "go.opentelemetry.io/collector/extension/experimental/storage"20)21var testStorageType config.Type = "test_storage"22// TestStorage is an in memory storage extension designed for testing23type TestStorage struct {24 config.ExtensionSettings25 storageDir string26}27// Ensure this storage extension implements the appropriate interface28var _ storage.Extension = (*TestStorage)(nil)29func NewStorageID(name string) config.ComponentID {30 return config.NewComponentIDWithName(testStorageType, name)31}32// NewInMemoryStorageExtension creates a TestStorage extension33func NewInMemoryStorageExtension(name string) *TestStorage {34 return &TestStorage{35 ExtensionSettings: config.NewExtensionSettings(36 NewStorageID(name),37 ),38 }39}40// NewFileBackedStorageExtension creates a TestStorage extension41func NewFileBackedStorageExtension(name string, storageDir string) *TestStorage {42 return &TestStorage{43 ExtensionSettings: config.NewExtensionSettings(44 NewStorageID(name),45 ),46 storageDir: storageDir,47 }48}49// Start does nothing50func (s *TestStorage) Start(context.Context, component.Host) error {51 return nil52}53// Shutdown does nothing54func (s *TestStorage) Shutdown(ctx context.Context) error {55 return nil56}57// GetClient returns a storage client for an individual component58func (s *TestStorage) GetClient(ctx context.Context, kind component.Kind, ent config.ComponentID, name string) (storage.Client, error) {59 var client *TestClient60 if s.storageDir == "" {61 client = NewInMemoryClient(kind, ent, name)62 } else {63 client = NewFileBackedClient(kind, ent, name, s.storageDir)64 }65 return client, setCreatorID(ctx, client, s.ID())66}67var nonStorageType config.Type = "non_storage"68// NonStorage is useful for testing expected behaviors that involve69// non-storage extensions70type NonStorage struct {71 config.ExtensionSettings72}...
TestStorage
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World")4 config := sample.Config{}5 config.TestStorage()6}7import (8func main() {9 fmt.Println("Hello World")10 config := sample.Config{}11 config.TestStorage()12}13import (14func main() {15 fmt.Println("Hello World")16 config := sample.Config{}17 config.TestStorage()18}19import (20func main() {21 fmt.Println("Hello World")22 config := sample.Config{}23 config.TestStorage()24}25import (26func main() {27 fmt.Println("Hello World")28 config := sample.Config{}29 config.TestStorage()30}31import (32func main() {33 fmt.Println("Hello World")34 config := sample.Config{}35 config.TestStorage()36}37import (38func main() {39 fmt.Println("Hello World")40 config := sample.Config{}41 config.TestStorage()42}43import (44func main() {45 fmt.Println("Hello World")46 config := sample.Config{}47 config.TestStorage()48}49import (50func main() {51 fmt.Println("Hello World")52 config := sample.Config{}53 config.TestStorage()54}55import (56func main() {57 fmt.Println("Hello World")58 config := sample.Config{}59 config.TestStorage()60}
TestStorage
Using AI Code Generation
1import (2func main() {3 fmt.Println("Testing storage")4 c := config.NewConfig()5 c.TestStorage()6}7import (8func main() {9 fmt.Println("Testing storage")10 c := config.NewConfig()11 c.TestStorage()12}13import (14func main() {15 fmt.Println("Testing storage")16 c := config.NewConfig()17 c.TestStorage()18}19import (20func main() {21 fmt.Println("Testing storage")22 c := config.NewConfig()23 c.TestStorage()24}25import (26func main() {27 fmt.Println("Testing storage")28 c := config.NewConfig()29 c.TestStorage()30}31import (32func main() {33 fmt.Println("Testing storage")34 c := config.NewConfig()35 c.TestStorage()36}37import (38func main() {39 fmt.Println("Testing storage")40 c := config.NewConfig()41 c.TestStorage()42}43import (44func main() {45 fmt.Println("Testing storage")46 c := config.NewConfig()47 c.TestStorage()48}49import (50func main() {51 fmt.Println("Testing storage")52 c := config.NewConfig()53 c.TestStorage()54}55import (56func main() {57 fmt.Println("Testing storage")58 c := config.NewConfig()
TestStorage
Using AI Code Generation
1import (2func main() {3 fmt.Println(config.TestStorage())4}5TestStorage() called6TestStorage() called7TestStorage() called8TestStorage() called9TestStorage() called10TestStorage() called11TestStorage() called12TestStorage() called
TestStorage
Using AI Code Generation
1import (2func main() {3 config := config.Config{}4 config.TestStorage()5}6import (7type Config struct {8}9func (c *Config) TestStorage() {10 fmt.Println("Storage test")11}12import (13type Storage struct {14}15func (s *Storage) Test() {16 fmt.Println("Storage test")17}18import (19func TestStorage(t *testing.T) {20 storage := Storage{}21 storage.Test()22}23import (24func TestConfig(t *testing.T) {25 config := Config{}26 config.TestStorage()27}28import (29type Storage struct {30}31func (s *Storage) Test() {32 fmt.Println("Storage test")33}34import (35func TestStorage(t *testing.T) {36 storage := Storage{}37 storage.Test()38}39import (40func TestConfig(t *testing.T) {41 config := Config{}42 config.TestStorage()43}44import (45type Storage struct {46}47func (s *Storage) Test() {48 fmt.Println("Storage test")49}50import (51func TestStorage(t *testing.T) {52 storage := Storage{}53 storage.Test()54}55import (56func TestConfig(t *testing.T) {57 config := Config{}58 config.TestStorage()59}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!