|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "net/http/httputil" |
| 9 | + "os/exec" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/sclevine/spec" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +var _ = suite("network/byoip-prefix/update", func(t *testing.T, when spec.G, it spec.S) { |
| 17 | + var ( |
| 18 | + expect *require.Assertions |
| 19 | + cmd *exec.Cmd |
| 20 | + server *httptest.Server |
| 21 | + ) |
| 22 | + |
| 23 | + it.Before(func() { |
| 24 | + expect = require.New(t) |
| 25 | + |
| 26 | + server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 27 | + switch req.URL.Path { |
| 28 | + case "/v2/byoip_prefixes/78d564a7-bc3f-4489-be14-1fb714969213": |
| 29 | + auth := req.Header.Get("Authorization") |
| 30 | + if auth != "Bearer some-magic-token" { |
| 31 | + w.WriteHeader(http.StatusUnauthorized) |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + if req.Method != http.MethodPatch { |
| 36 | + w.WriteHeader(http.StatusMethodNotAllowed) |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + reqBody, err := io.ReadAll(req.Body) |
| 41 | + expect.NoError(err) |
| 42 | + |
| 43 | + matchedRequest := byoipPrefixUpdateRequest |
| 44 | + responseJSON := byoipPrefixUpdateResponse |
| 45 | + |
| 46 | + expect.JSONEq(matchedRequest, string(reqBody)) |
| 47 | + |
| 48 | + w.Write([]byte(responseJSON)) |
| 49 | + default: |
| 50 | + dump, err := httputil.DumpRequest(req, true) |
| 51 | + if err != nil { |
| 52 | + t.Fatal("failed to dump request") |
| 53 | + } |
| 54 | + |
| 55 | + t.Fatalf("received unknown request: %s", dump) |
| 56 | + } |
| 57 | + })) |
| 58 | + }) |
| 59 | + |
| 60 | + when("the valid request", func() { |
| 61 | + it("updates the byoip prefix", func() { |
| 62 | + aliases := []string{"update", "u"} |
| 63 | + |
| 64 | + for _, alias := range aliases { |
| 65 | + cmd = exec.Command(builtBinaryPath, |
| 66 | + "-t", "some-magic-token", |
| 67 | + "-u", server.URL, |
| 68 | + "network", |
| 69 | + "byoip-prefix", |
| 70 | + alias, |
| 71 | + "78d564a7-bc3f-4489-be14-1fb714969213", |
| 72 | + "--advertise=true", |
| 73 | + ) |
| 74 | + |
| 75 | + output, err := cmd.CombinedOutput() |
| 76 | + expect.NoError(err, fmt.Sprintf("received error output: %s", output)) |
| 77 | + expect.Equal(norm(byoipPrefixUpdateOutput), norm(string(output))) |
| 78 | + } |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + when("advertise is set to false", func() { |
| 83 | + it("updates the byoip prefix to disable advertisement", func() { |
| 84 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 85 | + switch req.URL.Path { |
| 86 | + case "/v2/byoip_prefixes/78d564a7-bc3f-4489-be14-1fb714969213": |
| 87 | + auth := req.Header.Get("Authorization") |
| 88 | + if auth != "Bearer some-magic-token" { |
| 89 | + w.WriteHeader(http.StatusUnauthorized) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + if req.Method != http.MethodPatch { |
| 94 | + w.WriteHeader(http.StatusMethodNotAllowed) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + reqBody, err := io.ReadAll(req.Body) |
| 99 | + expect.NoError(err) |
| 100 | + |
| 101 | + matchedRequest := byoipPrefixUpdateRequestFalse |
| 102 | + responseJSON := byoipPrefixUpdateResponseFalse |
| 103 | + |
| 104 | + expect.JSONEq(matchedRequest, string(reqBody)) |
| 105 | + |
| 106 | + w.Write([]byte(responseJSON)) |
| 107 | + default: |
| 108 | + dump, err := httputil.DumpRequest(req, true) |
| 109 | + if err != nil { |
| 110 | + t.Fatal("failed to dump request") |
| 111 | + } |
| 112 | + |
| 113 | + t.Fatalf("received unknown request: %s", dump) |
| 114 | + } |
| 115 | + })) |
| 116 | + defer server.Close() |
| 117 | + |
| 118 | + cmd = exec.Command(builtBinaryPath, |
| 119 | + "-t", "some-magic-token", |
| 120 | + "-u", server.URL, |
| 121 | + "network", |
| 122 | + "byoip-prefix", |
| 123 | + "update", |
| 124 | + "78d564a7-bc3f-4489-be14-1fb714969213", |
| 125 | + "--advertise=false", |
| 126 | + ) |
| 127 | + |
| 128 | + output, err := cmd.CombinedOutput() |
| 129 | + expect.NoError(err, fmt.Sprintf("received error output: %s", output)) |
| 130 | + expect.Equal(norm(byoipPrefixUpdateOutputFalse), norm(string(output))) |
| 131 | + }) |
| 132 | + }) |
| 133 | +}) |
| 134 | + |
| 135 | +const ( |
| 136 | + byoipPrefixUpdateOutput = ` |
| 137 | +Prefix Region Status UUID Advertised Failure Reason Validations |
| 138 | +10.1.1.1/24 nyc3 active 78d564a7-bc3f-4489-be14-1fb714969213 true [] |
| 139 | +` |
| 140 | + byoipPrefixUpdateResponse = ` |
| 141 | +{ |
| 142 | +"byoip_prefix": { |
| 143 | + "uuid": "78d564a7-bc3f-4489-be14-1fb714969213", |
| 144 | + "region": "nyc3", |
| 145 | + "status": "active", |
| 146 | + "prefix": "10.1.1.1/24", |
| 147 | + "advertised": true, |
| 148 | + "validations": [], |
| 149 | + "failure_reason": "" |
| 150 | +} |
| 151 | +} |
| 152 | +` |
| 153 | + |
| 154 | + byoipPrefixUpdateRequest = ` |
| 155 | +{"advertise":true} |
| 156 | +` |
| 157 | + |
| 158 | + byoipPrefixUpdateOutputFalse = ` |
| 159 | +Prefix Region Status UUID Advertised Failure Reason Validations |
| 160 | +10.1.1.1/24 nyc3 active 78d564a7-bc3f-4489-be14-1fb714969213 false [] |
| 161 | +` |
| 162 | + byoipPrefixUpdateResponseFalse = ` |
| 163 | +{ |
| 164 | +"byoip_prefix": { |
| 165 | + "uuid": "78d564a7-bc3f-4489-be14-1fb714969213", |
| 166 | + "region": "nyc3", |
| 167 | + "status": "active", |
| 168 | + "prefix": "10.1.1.1/24", |
| 169 | + "advertised": false, |
| 170 | + "validations": [], |
| 171 | + "failure_reason": "" |
| 172 | +} |
| 173 | +} |
| 174 | +` |
| 175 | + |
| 176 | + byoipPrefixUpdateRequestFalse = ` |
| 177 | +{"advertise":false} |
| 178 | +` |
| 179 | +) |
0 commit comments