-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: add IPAMClaimReference to network-selection-elements
Also add some unit tests to assert users cannot both request a particular IP address and reference an IPAMClaim object at the same time. Signed-off-by: Miguel Duarte Barroso <mdbarroso@redhat.com>
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package v1 | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
type testCase struct { | ||
description string | ||
input string | ||
expectedOutput NetworkSelectionElement | ||
expectedError error | ||
} | ||
|
||
func TestNetworkSelectionElementUnmarshaller(t *testing.T) { | ||
|
||
testCases := []testCase{ | ||
{ | ||
description: "ip request + IPAMClaims", | ||
input: "{\"name\":\"yo!\",\"ips\":[\"asd\"],\"ipam-claim-reference\":\"woop\"}", | ||
expectedError: TooManyIPSources, | ||
}, | ||
{ | ||
description: "successfully deserialize a simple struct", | ||
input: "{\"name\":\"yo!\",\"ips\":[\"an IP\"]}", | ||
expectedOutput: NetworkSelectionElement{ | ||
Name: "yo!", | ||
IPRequest: []string{"an IP"}, | ||
}, | ||
expectedError: nil, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.description, func(t *testing.T) { | ||
if err := run(tc); err != nil { | ||
t.Errorf("failed test %q: %v", tc.description, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func run(tc testCase) error { | ||
inputBytes := []byte(tc.input) | ||
|
||
var nse NetworkSelectionElement | ||
err := json.Unmarshal(inputBytes, &nse) | ||
if tc.expectedError != nil { | ||
if !errors.Is(err, tc.expectedError) { | ||
return fmt.Errorf("unexpected error: %v. Expected error: %v", err, tc.expectedError) | ||
} | ||
} | ||
if !reflect.DeepEqual(nse, tc.expectedOutput) { | ||
return fmt.Errorf("parsed object is wrong: %v. Expected object: %v", nse, tc.expectedOutput) | ||
} | ||
return nil | ||
} |