|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package agency |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + "strings" |
| 26 | + "testing" |
| 27 | + |
| 28 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 29 | + "github.com/dchest/uniuri" |
| 30 | + "github.com/stretchr/testify/require" |
| 31 | +) |
| 32 | + |
| 33 | +func NewDatabaseRandomGenerator() DatabaseGeneratorInterface { |
| 34 | + return NewDatabaseGenerator(fmt.Sprintf("d%s", strings.ToLower(uniuri.NewLen(16)))) |
| 35 | +} |
| 36 | + |
| 37 | +func NewDatabaseGenerator(name string) DatabaseGeneratorInterface { |
| 38 | + return databaseGenerator{ |
| 39 | + db: name, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +type DatabaseGeneratorInterface interface { |
| 44 | + Collection(name string) CollectionGeneratorInterface |
| 45 | + RandomCollection() CollectionGeneratorInterface |
| 46 | + Add() StateGenerator |
| 47 | +} |
| 48 | + |
| 49 | +type databaseGenerator struct { |
| 50 | + db string |
| 51 | + |
| 52 | + collections map[string]collectionGenerator |
| 53 | +} |
| 54 | + |
| 55 | +func (d databaseGenerator) RandomCollection() CollectionGeneratorInterface { |
| 56 | + return d.Collection(fmt.Sprintf("c%s", strings.ToLower(uniuri.NewLen(16)))) |
| 57 | +} |
| 58 | + |
| 59 | +func (d databaseGenerator) Collection(name string) CollectionGeneratorInterface { |
| 60 | + return collectionGenerator{ |
| 61 | + db: d, |
| 62 | + col: name, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func (d databaseGenerator) Add() StateGenerator { |
| 67 | + return func(t *testing.T, s *State) { |
| 68 | + if s.Plan.Collections == nil { |
| 69 | + s.Plan.Collections = StatePlanCollections{} |
| 70 | + } |
| 71 | + |
| 72 | + if s.Current.Collections == nil { |
| 73 | + s.Current.Collections = StateCurrentCollections{} |
| 74 | + } |
| 75 | + |
| 76 | + _, ok := s.Plan.Collections[d.db] |
| 77 | + require.False(t, ok) |
| 78 | + |
| 79 | + _, ok = s.Current.Collections[d.db] |
| 80 | + require.False(t, ok) |
| 81 | + |
| 82 | + plan := StatePlanDBCollections{} |
| 83 | + current := StateCurrentDBCollections{} |
| 84 | + |
| 85 | + for col, colDet := range d.collections { |
| 86 | + planShards := Shards{} |
| 87 | + currentShards := StateCurrentDBCollection{} |
| 88 | + |
| 89 | + for shard, shardDet := range colDet.shards { |
| 90 | + n := fmt.Sprintf("s%d", shard) |
| 91 | + |
| 92 | + planShards[n] = shardDet.plan |
| 93 | + currentShards[n] = StateCurrentDBShard{Servers: shardDet.current} |
| 94 | + } |
| 95 | + |
| 96 | + planCol := StatePlanCollection{ |
| 97 | + Name: util.NewString(col), |
| 98 | + Shards: planShards, |
| 99 | + WriteConcern: colDet.wc, |
| 100 | + ReplicationFactor: colDet.rf, |
| 101 | + } |
| 102 | + |
| 103 | + plan[col] = planCol |
| 104 | + current[col] = currentShards |
| 105 | + } |
| 106 | + |
| 107 | + s.Plan.Collections[d.db] = plan |
| 108 | + s.Current.Collections[d.db] = current |
| 109 | + } |
| 110 | +} |
0 commit comments