github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/client/internal/config/manager_test.go (about) 1 // Copyright 2017 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package config 16 17 import ( 18 "path/filepath" 19 "sync" 20 "testing" 21 22 "github.com/google/fleetspeak/fleetspeak/src/client/config" 23 "github.com/google/fleetspeak/fleetspeak/src/client/stats" 24 "github.com/google/fleetspeak/fleetspeak/src/common" 25 "github.com/google/fleetspeak/fleetspeak/src/comtesting" 26 27 fspb "github.com/google/fleetspeak/fleetspeak/src/common/proto/fleetspeak" 28 ) 29 30 // statsCollector implements stats.ConfigManagerStatsCollector 31 type statsCollector struct { 32 stats.ConfigManagerCollector 33 mu sync.Mutex 34 rekeys int 35 } 36 37 func (sc *statsCollector) AfterRekey(err error) { 38 sc.mu.Lock() 39 defer sc.mu.Unlock() 40 sc.rekeys++ 41 } 42 43 func (sc *statsCollector) AfterConfigSync(err error) { 44 45 } 46 47 func TestRekey(t *testing.T) { 48 sc := &statsCollector{} 49 m, err := StartManager(&config.Configuration{ 50 FixedServices: make([]*fspb.ClientServiceConfig, 0), 51 }, make(chan *fspb.ClientInfoData), sc) 52 if err != nil { 53 t.Errorf("unable to create config manager: %v", err) 54 return 55 } 56 defer m.Stop() 57 58 id1 := m.ClientID() 59 if (id1 == common.ClientID{}) { 60 t.Errorf("new config manager should provide non-trivial ClientID") 61 } 62 if err := m.Rekey(); err != nil { 63 t.Errorf("unable to rekey: %v", err) 64 } 65 id2 := m.ClientID() 66 if (id2 == common.ClientID{}) || id2 == id1 { 67 t.Errorf("ClientID after rekey is %v, expected to be non-trivial and different from %v", id2, id1) 68 } 69 sc.mu.Lock() 70 defer sc.mu.Unlock() 71 // 1 initial attempt, 1 explicit attempt 72 if sc.rekeys != 2 { 73 t.Errorf("Unexpected amount of rekeys reported, got: %d, want: 2", sc.rekeys) 74 } 75 } 76 77 func TestWriteback(t *testing.T) { 78 tmpPath, fin := comtesting.GetTempDir("TestWriteback") 79 defer fin() 80 81 ph, err := config.NewFilesystemPersistenceHandler(tmpPath, filepath.Join(tmpPath, "writeback")) 82 if err != nil { 83 t.Fatal(err) 84 } 85 86 m1, err := StartManager(&config.Configuration{ 87 PersistenceHandler: ph, 88 }, make(chan *fspb.ClientInfoData), &statsCollector{}) 89 if err != nil { 90 t.Errorf("unable to create config manager: %v", err) 91 return 92 } 93 id1 := m1.ClientID() 94 if (id1 == common.ClientID{}) { 95 t.Errorf("New config manager should provide non-trivial ClientID.") 96 } 97 m1.Stop() 98 99 ph, err = config.NewFilesystemPersistenceHandler(tmpPath, filepath.Join(tmpPath, "writeback")) 100 if err != nil { 101 t.Fatal(err) 102 } 103 104 m2, err := StartManager(&config.Configuration{ 105 PersistenceHandler: ph, 106 }, make(chan *fspb.ClientInfoData), &statsCollector{}) 107 if err != nil { 108 t.Errorf("Unable to create new config manager: %v", err) 109 return 110 } 111 defer m2.Stop() 112 id2 := m2.ClientID() 113 if id2 != id1 { 114 t.Errorf("Got clientID=%v in reconstituted config, expected %v", id2, id1) 115 } 116 }