|
8 | 8 |
|
9 | 9 | "github.com/canonical/microk8s-cluster-agent/pkg/snap/mock" |
10 | 10 | snaputil "github.com/canonical/microk8s-cluster-agent/pkg/snap/util" |
| 11 | + . "github.com/onsi/gomega" |
11 | 12 | ) |
12 | 13 |
|
13 | 14 | func TestUpdateDqliteIP(t *testing.T) { |
@@ -84,3 +85,79 @@ Role: 0`, |
84 | 85 | }) |
85 | 86 |
|
86 | 87 | } |
| 88 | + |
| 89 | +func TestUpdateDqliteBindAddress(t *testing.T) { |
| 90 | + findMatchingBindAddressMock := func(addr string) (string, error) { |
| 91 | + return "10.10.10.10", nil |
| 92 | + } |
| 93 | + |
| 94 | + t.Run("MustFailIfNodeAlreadyKnown", func(t *testing.T) { |
| 95 | + |
| 96 | + s := &mock.Snap{ |
| 97 | + DqliteClusterYaml: ` |
| 98 | +- Address: 127.0.0.1:19001 |
| 99 | + ID: 1236189235178654365 |
| 100 | + Role: 0`, |
| 101 | + DqliteInfoYaml: ` |
| 102 | +Address: 127.0.0.1:19001 |
| 103 | +ID: 1236189235178654365 |
| 104 | +Role: 0`, |
| 105 | + } |
| 106 | + |
| 107 | + g := NewWithT(t) |
| 108 | + err := snaputil.MaybeUpdateDqliteBindAddress(context.Background(), s, "127.0.0.1", "127.0.0.1", findMatchingBindAddressMock) |
| 109 | + g.Expect(err).To(MatchError("the joining node (127.0.0.1) is already known to dqlite")) |
| 110 | + |
| 111 | + }) |
| 112 | + |
| 113 | + t.Run("MustNotUpdateIfMultipleNodes", func(t *testing.T) { |
| 114 | + s := &mock.Snap{ |
| 115 | + DqliteClusterYaml: ` |
| 116 | +- Address: 127.0.0.1:19001 |
| 117 | + ID: 1236189235178654365 |
| 118 | + Role: 0 |
| 119 | +- Address: 10.10.10.10:19001 |
| 120 | + ID: 12345678987654321 |
| 121 | + Role: 0`, |
| 122 | + DqliteInfoYaml: ` |
| 123 | +Address: 127.0.0.1:19001 |
| 124 | +ID: 1236189235178654365 |
| 125 | +Role: 0`, |
| 126 | + } |
| 127 | + |
| 128 | + g := NewWithT(t) |
| 129 | + err := snaputil.MaybeUpdateDqliteBindAddress(context.Background(), s, "127.0.0.1:19001", "8.8.8.8", findMatchingBindAddressMock) |
| 130 | + g.Expect(err).To(BeNil()) |
| 131 | + g.Expect(s.WriteDqliteUpdateYamlCalledWith).To(BeEmpty()) |
| 132 | + }) |
| 133 | + |
| 134 | + t.Run("MustUpdateIfOneNodeAndBindsLocal", func(t *testing.T) { |
| 135 | + s := &mock.Snap{ |
| 136 | + DqliteClusterYaml: ` |
| 137 | +- Address: 127.0.0.1:19001 |
| 138 | + ID: 1236189235178654365 |
| 139 | + Role: 0`, |
| 140 | + DqliteInfoYaml: ` |
| 141 | +Address: 127.0.0.1:19001 |
| 142 | +ID: 1236189235178654365 |
| 143 | +Role: 0`, |
| 144 | + } |
| 145 | + // Update cluster yaml asynchronously |
| 146 | + // The mock snap implementation doesn't actually write the yaml but instead |
| 147 | + // sets `WriteDqliteUpdateYamlCalledWith`. |
| 148 | + // Thus, we need to manually change the config as otherwise `WaitForDqliteCluster` |
| 149 | + // would wait forever. |
| 150 | + go func() { |
| 151 | + <-time.After(500 * time.Millisecond) |
| 152 | + s.DqliteClusterYaml = ` |
| 153 | +- Address: 10.10.10.10:19001 |
| 154 | + ID: 1236189235178654365 |
| 155 | + Role: 0` |
| 156 | + }() |
| 157 | + |
| 158 | + g := NewWithT(t) |
| 159 | + err := snaputil.MaybeUpdateDqliteBindAddress(context.Background(), s, "127.0.0.1:19001", "10.10.10.10", findMatchingBindAddressMock) |
| 160 | + g.Expect(err).To(BeNil()) |
| 161 | + g.Expect(s.WriteDqliteUpdateYamlCalledWith).To(ConsistOf("Address: 10.10.10.10:19001\n")) |
| 162 | + }) |
| 163 | +} |
0 commit comments