-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
At some scenarios is useful to have a "structured" version of the network_state, this change creates a golang api tested toward examples and stated used at integration tests it also add the kubebuilder tags to be k8s CRD friendly. Signed-off-by: Enrique Llorente <ellorent@redhat.com>
- Loading branch information
Showing
23 changed files
with
3,980 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,5 @@ doc/nmstate-autoconf.8 | |
|
||
nmstate-*.tar.gz | ||
nmstate-*.tar.gz.asc | ||
|
||
.states |
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
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
--- | ||
dns-resolver: | ||
config: {} | ||
interfaces: [] |
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,24 @@ | ||
--- | ||
interfaces: | ||
- name: eth1 | ||
type: ethernet | ||
state: up | ||
- name: linux-br0 | ||
type: linux-bridge | ||
state: up | ||
linux-bridge: | ||
options: | ||
group-forward-mask: 0 | ||
mac-ageing-time: 300 | ||
multicast-snooping: true | ||
stp: | ||
enabled: true | ||
forward-delay: 15 | ||
hello-time: 2 | ||
max-age: 20 | ||
priority: 32768 | ||
port: | ||
- name: eth1 | ||
stp-hairpin-mode: false | ||
stp-path-cost: 100 | ||
stp-priority: 32 |
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,25 @@ | ||
--- | ||
interfaces: | ||
- name: eth1 | ||
type: ethernet | ||
state: up | ||
- name: ovs0 | ||
type: ovs-interface | ||
state: up | ||
ipv4: | ||
enabled: true | ||
address: | ||
- ip: 192.0.2.1 | ||
prefix-length: 24 | ||
- name: ovs-br0 | ||
type: ovs-bridge | ||
state: up | ||
ovs-bridge: | ||
options: | ||
fail-mode: '' | ||
mcast-snooping-enable: false | ||
rstp: false | ||
stp: true | ||
port: | ||
- name: eth1 | ||
- name: ovs0 |
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 |
---|---|---|
|
@@ -3,4 +3,5 @@ members = [ | |
"src/cli", | ||
"src/clib", | ||
"src/lib", | ||
"src/go/apigen", | ||
] |
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,79 @@ | ||
package nmstate | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/fs" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"sigs.k8s.io/yaml" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUnmarshallingExamples(t *testing.T) { | ||
examples := map[string][]byte{} | ||
err := filepath.Walk("../../../../examples", func(path string, info fs.FileInfo, err error) error { | ||
if info.IsDir() && info.Name() == "policy" { | ||
return filepath.SkipDir | ||
} | ||
if info.IsDir() || filepath.Ext(info.Name()) != ".yml" { | ||
return nil | ||
} | ||
example, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return fmt.Errorf("failed reading example '%s': %v", info.Name(), err) | ||
} | ||
examples[info.Name()] = example | ||
return nil | ||
}) | ||
require.NoError(t, err, "must succeed reading examples") | ||
for exampleName, expectedExample := range examples { | ||
t.Run(exampleName, func(t *testing.T) { | ||
netState := &NetworkState{} | ||
err := yaml.Unmarshal(expectedExample, netState) | ||
assert.NoError(t, err, "must success unmarshaling example") | ||
|
||
obtainedExample, err := yaml.Marshal(netState) | ||
assert.NoError(t, err, "must success marshaling example") | ||
|
||
assert.YAMLEq(t, string(expectedExample), string(obtainedExample), "expected: \n%s\nobtained: \n%s\n", expectedExample, obtainedExample) | ||
}) | ||
} | ||
} | ||
|
||
func TestUnmarshallingIntegrationTests(t *testing.T) { | ||
integrationTestsStatesPath := "../../../../tests/integration/.states/" | ||
testStates := map[string][]byte{} | ||
if _, err := os.Stat(integrationTestsStatesPath); os.IsNotExist(err) { | ||
t.Skipf("Missing integration test states dir %s", integrationTestsStatesPath) | ||
} | ||
err := filepath.Walk(integrationTestsStatesPath, func(path string, info fs.FileInfo, err error) error { | ||
if info.IsDir() || filepath.Ext(info.Name()) != ".json" { | ||
return nil | ||
} | ||
testState, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return fmt.Errorf("failed reading integration test '%s': %v", info.Name(), err) | ||
} | ||
testStates[info.Name()] = testState | ||
return nil | ||
}) | ||
require.NoError(t, err, "must succeed reading examples") | ||
for testStateName, expectedTestState := range testStates { | ||
t.Run(testStateName, func(t *testing.T) { | ||
netState := &NetworkState{} | ||
err := json.Unmarshal(expectedTestState, netState) | ||
assert.NoError(t, err, "must success unmarshaling test state") | ||
|
||
obtainedTestState, err := json.Marshal(netState) | ||
assert.NoError(t, err, "must success marshaling test state") | ||
|
||
assert.JSONEq(t, string(expectedTestState), string(obtainedTestState), "expected: \n%s\nobtained: \n%s\n", expectedTestState, obtainedTestState) | ||
}) | ||
} | ||
} |
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,16 @@ | ||
/* | ||
Copyright The NMState Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
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,3 @@ | ||
#!/bin/bash -e | ||
|
||
GOFLAGS=-mod=mod go run sigs.k8s.io/controller-tools/cmd/controller-gen@v0.8.0 $@ |
Oops, something went wrong.