|
| 1 | +package agollo |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestOptions(t *testing.T) { |
| 11 | + var ( |
| 12 | + configServerURL = "localhost:8080" |
| 13 | + appID = "SampleApp" |
| 14 | + ) |
| 15 | + var tests = []struct { |
| 16 | + Options []Option |
| 17 | + Check func(Options) |
| 18 | + }{ |
| 19 | + { |
| 20 | + []Option{}, |
| 21 | + func(opts Options) { |
| 22 | + assert.Equal(t, appID, opts.AppID) |
| 23 | + assert.Equal(t, defaultCluster, opts.Cluster) |
| 24 | + assert.Equal(t, defaultAutoFetchOnCacheMiss, opts.AutoFetchOnCacheMiss) |
| 25 | + assert.Equal(t, defaultLongPollInterval, opts.LongPollerInterval) |
| 26 | + assert.Equal(t, defaultBackupFile, opts.BackupFile) |
| 27 | + assert.Equal(t, defaultFailTolerantOnBackupExists, opts.FailTolerantOnBackupExists) |
| 28 | + assert.Equal(t, defaultEnableSLB, opts.EnableSLB) |
| 29 | + assert.NotNil(t, opts.Logger) |
| 30 | + assert.NotNil(t, opts.ApolloClient) |
| 31 | + assert.NotNil(t, opts.Balancer) |
| 32 | + assert.Empty(t, opts.PreloadNamespaces) |
| 33 | + assert.Empty(t, opts.DefaultNamespace) |
| 34 | + getOpts := opts.newGetOptions() |
| 35 | + assert.Equal(t, "application", getOpts.Namespace) |
| 36 | + getOpts = opts.newGetOptions(WithNamespace("customize_namespace")) |
| 37 | + assert.Equal(t, "customize_namespace", getOpts.Namespace) |
| 38 | + ac := &apolloClient{} |
| 39 | + ac.Apply(opts.ClientOptions...) |
| 40 | + assert.Empty(t, ac.AccessKey) |
| 41 | + }, |
| 42 | + }, |
| 43 | + { |
| 44 | + []Option{ |
| 45 | + Cluster("test_cluster"), |
| 46 | + DefaultNamespace("default_namespace"), |
| 47 | + PreloadNamespaces("preload_namespace"), |
| 48 | + AutoFetchOnCacheMiss(), |
| 49 | + LongPollerInterval(time.Second * 30), |
| 50 | + BackupFile("test_backup"), |
| 51 | + FailTolerantOnBackupExists(), |
| 52 | + AccessKey("test_access_key"), |
| 53 | + }, |
| 54 | + func(opts Options) { |
| 55 | + assert.Equal(t, "test_cluster", opts.Cluster) |
| 56 | + assert.Equal(t, []string{"preload_namespace", "default_namespace"}, opts.PreloadNamespaces) |
| 57 | + assert.Equal(t, "default_namespace", opts.DefaultNamespace) |
| 58 | + getOpts := opts.newGetOptions() |
| 59 | + assert.Equal(t, "default_namespace", getOpts.Namespace) |
| 60 | + getOpts = opts.newGetOptions(WithNamespace("customize_namespace")) |
| 61 | + assert.Equal(t, "customize_namespace", getOpts.Namespace) |
| 62 | + assert.Equal(t, true, opts.AutoFetchOnCacheMiss) |
| 63 | + assert.Equal(t, time.Second*30, opts.LongPollerInterval) |
| 64 | + assert.Equal(t, "test_backup", opts.BackupFile) |
| 65 | + assert.Equal(t, true, opts.FailTolerantOnBackupExists) |
| 66 | + ac := &apolloClient{} |
| 67 | + ac.Apply(opts.ClientOptions...) |
| 68 | + assert.Equal(t, "test_access_key", ac.AccessKey) |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + []Option{ |
| 73 | + EnableSLB(true), |
| 74 | + WithApolloClient(&mockApolloClient{ |
| 75 | + getConfigServers: func(metaServerURL, appID string) (i int, servers []ConfigServer, err error) { |
| 76 | + return 200, |
| 77 | + []ConfigServer{ |
| 78 | + ConfigServer{ |
| 79 | + AppName: "test", |
| 80 | + InstanceID: "test", |
| 81 | + HomePageURL: "http://localhost:8080", |
| 82 | + }, |
| 83 | + }, |
| 84 | + nil |
| 85 | + }, |
| 86 | + }), |
| 87 | + }, |
| 88 | + func(opts Options) { |
| 89 | + assert.Equal(t, true, opts.EnableSLB) |
| 90 | + }, |
| 91 | + }, |
| 92 | + } |
| 93 | + |
| 94 | + for _, test := range tests { |
| 95 | + opts, err := newOptions(configServerURL, appID, test.Options...) |
| 96 | + if err != nil { |
| 97 | + assert.Nil(t, err) |
| 98 | + } |
| 99 | + test.Check(opts) |
| 100 | + } |
| 101 | +} |
0 commit comments