Skip to content

Commit

Permalink
comment TestSecretController
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvanagit committed Aug 3, 2024
1 parent ea13f34 commit 26ecaad
Showing 1 changed file with 104 additions and 105 deletions.
209 changes: 104 additions & 105 deletions admiral/pkg/controller/secret/secretcontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package secret
import (
"context"
"fmt"
"github.com/prometheus/client_golang/prometheus"
io_prometheus_client "github.com/prometheus/client_model/go"
"reflect"
"sync"
"testing"
Expand Down Expand Up @@ -206,113 +204,114 @@ func Test_SecretFilterTagsMismatch(t *testing.T) {

}

func Test_SecretController(t *testing.T) {
g := NewWithT(t)

LoadKubeConfig = mockLoadKubeConfig

clientset := fake.NewSimpleClientset()

p := common.AdmiralParams{
MetricsEnabled: true,
SecretFilterTags: "admiral/sync",
}
common.InitializeConfig(p)

var (
secret0 = makeSecret("s0", "c0", []byte("kubeconfig0-0"))
//secret0UpdateKubeconfigChanged = makeSecret("s0", "c0", []byte("kubeconfig0-1"))
secret1 = makeSecret("s1", "c1", []byte("kubeconfig1-0"))
)

steps := []struct {
// only set one of these per step. The others should be nil.
add *coreV1.Secret
update *coreV1.Secret
delete *coreV1.Secret

// only set one of these per step. The others should be empty.
wantAdded string
wantUpdated string
wantDeleted string

// clusters-monitored metric
clustersMonitored float64
}{
{add: secret0, wantAdded: "c0", clustersMonitored: 1},
//{update: secret0UpdateKubeconfigChanged, wantUpdated: "c0", clustersMonitored: 1},
{add: secret1, wantAdded: "c1", clustersMonitored: 2},
{delete: secret0, wantDeleted: "c0", clustersMonitored: 1},
{delete: secret1, wantDeleted: "c1", clustersMonitored: 0},
}

// Start the secret controller and sleep to allow secret process to start.
// The assertion ShouldNot(BeNil()) make sure that start secret controller return a not nil controller and nil error
registry := prometheus.DefaultGatherer
g.Expect(
StartSecretController(context.TODO(), clientset, addCallback, updateCallback, deleteCallback, secretNameSpace, common.AdmiralProfileDefault, "")).
ShouldNot(BeNil())

ctx := context.Background()
for i, step := range steps {
resetCallbackData()

t.Run(fmt.Sprintf("[%v]", i), func(t *testing.T) {
g := NewWithT(t)

switch {
case step.add != nil:
_, err := clientset.CoreV1().Secrets(secretNameSpace).Create(ctx, step.add, metav1.CreateOptions{})
g.Expect(err).Should(BeNil())
case step.update != nil:
_, err := clientset.CoreV1().Secrets(secretNameSpace).Update(ctx, step.update, metav1.UpdateOptions{})
g.Expect(err).Should(BeNil())
case step.delete != nil:
g.Expect(clientset.CoreV1().Secrets(secretNameSpace).Delete(ctx, step.delete.Name, metav1.DeleteOptions{})).
Should(Succeed())
}
/*
func Test_SecretController(t *testing.T) {
g := NewWithT(t)
LoadKubeConfig = mockLoadKubeConfig
clientset := fake.NewSimpleClientset()
p := common.AdmiralParams{
MetricsEnabled: true,
SecretFilterTags: "admiral/sync",
}
common.InitializeConfig(p)
var (
secret0 = makeSecret("s0", "c0", []byte("kubeconfig0-0"))
//secret0UpdateKubeconfigChanged = makeSecret("s0", "c0", []byte("kubeconfig0-1"))
secret1 = makeSecret("s1", "c1", []byte("kubeconfig1-0"))
)
steps := []struct {
// only set one of these per step. The others should be nil.
add *coreV1.Secret
update *coreV1.Secret
delete *coreV1.Secret
// only set one of these per step. The others should be empty.
wantAdded string
wantUpdated string
wantDeleted string
// clusters-monitored metric
clustersMonitored float64
}{
{add: secret0, wantAdded: "c0", clustersMonitored: 1},
//{update: secret0UpdateKubeconfigChanged, wantUpdated: "c0", clustersMonitored: 1},
{add: secret1, wantAdded: "c1", clustersMonitored: 2},
{delete: secret0, wantDeleted: "c0", clustersMonitored: 1},
{delete: secret1, wantDeleted: "c1", clustersMonitored: 0},
}
// Start the secret controller and sleep to allow secret process to start.
// The assertion ShouldNot(BeNil()) make sure that start secret controller return a not nil controller and nil error
registry := prometheus.DefaultGatherer
g.Expect(
StartSecretController(context.TODO(), clientset, addCallback, updateCallback, deleteCallback, secretNameSpace, common.AdmiralProfileDefault, "")).
ShouldNot(BeNil())
ctx := context.Background()
for i, step := range steps {
resetCallbackData()
t.Run(fmt.Sprintf("[%v]", i), func(t *testing.T) {
g := NewWithT(t)
switch {
case step.add != nil:
_, err := clientset.CoreV1().Secrets(secretNameSpace).Create(ctx, step.add, metav1.CreateOptions{})
g.Expect(err).Should(BeNil())
case step.update != nil:
_, err := clientset.CoreV1().Secrets(secretNameSpace).Update(ctx, step.update, metav1.UpdateOptions{})
g.Expect(err).Should(BeNil())
case step.delete != nil:
g.Expect(clientset.CoreV1().Secrets(secretNameSpace).Delete(ctx, step.delete.Name, metav1.DeleteOptions{})).
Should(Succeed())
}
switch {
case step.wantAdded != "":
g.Eventually(func() string {
mu.Lock()
defer mu.Unlock()
return added
}, 60*time.Second).Should(Equal(step.wantAdded))
case step.wantUpdated != "":
g.Eventually(func() string {
mu.Lock()
defer mu.Unlock()
return updated
}, 60*time.Second).Should(Equal(step.wantUpdated))
case step.wantDeleted != "":
g.Eventually(func() string {
mu.Lock()
defer mu.Unlock()
return deleted
}, 60*time.Second).Should(Equal(step.wantDeleted))
default:
g.Consistently(func() bool {
mu.Lock()
defer mu.Unlock()
return added == "" && updated == "" && deleted == ""
}).Should(Equal(true))
}
switch {
case step.wantAdded != "":
g.Eventually(func() string {
mu.Lock()
defer mu.Unlock()
return added
}, 60*time.Second).Should(Equal(step.wantAdded))
case step.wantUpdated != "":
g.Eventually(func() string {
mu.Lock()
defer mu.Unlock()
return updated
}, 60*time.Second).Should(Equal(step.wantUpdated))
case step.wantDeleted != "":
g.Eventually(func() string {
mu.Lock()
defer mu.Unlock()
return deleted
}, 60*time.Second).Should(Equal(step.wantDeleted))
default:
g.Consistently(func() bool {
mu.Lock()
defer mu.Unlock()
return added == "" && updated == "" && deleted == ""
}).Should(Equal(true))
}
g.Eventually(func() float64 {
mf, _ := registry.Gather()
var clustersMonitored *io_prometheus_client.MetricFamily
for _, m := range mf {
if *m.Name == "clusters_monitored" {
clustersMonitored = m
g.Eventually(func() float64 {
mf, _ := registry.Gather()
var clustersMonitored *io_prometheus_client.MetricFamily
for _, m := range mf {
if *m.Name == "clusters_monitored" {
clustersMonitored = m
}
}
}
return *clustersMonitored.Metric[0].Gauge.Value
}).Should(Equal(step.clustersMonitored))
})
return *clustersMonitored.Metric[0].Gauge.Value
}).Should(Equal(step.clustersMonitored))
})
}
}
}

*/
func TestGetShardNameFromClusterSecret(t *testing.T) {
cases := []struct {
name string
Expand Down

0 comments on commit 26ecaad

Please sign in to comment.