github.com/nmstate/kubernetes-nmstate@v0.82.0/test/e2e/handler/states.go (about)

     1  /*
     2  Copyright The Kubernetes NMState Authors.
     3  
     4  
     5  Licensed under the Apache License, Version 2.0 (the "License");
     6  you may not use this file except in compliance with the License.
     7  You may obtain a copy of the License at
     8  
     9      http://www.apache.org/licenses/LICENSE-2.0
    10  
    11  Unless required by applicable law or agreed to in writing, software
    12  distributed under the License is distributed on an "AS IS" BASIS,
    13  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  See the License for the specific language governing permissions and
    15  limitations under the License.
    16  */
    17  
    18  package handler
    19  
    20  import (
    21  	"fmt"
    22  	"strings"
    23  	"text/template"
    24  
    25  	. "github.com/onsi/gomega"
    26  	"github.com/onsi/gomega/types"
    27  
    28  	nmstate "github.com/nmstate/kubernetes-nmstate/api/shared"
    29  )
    30  
    31  func ethernetNicsState(states map[string]string) nmstate.State {
    32  	tmp, err := template.New("ethernetNicsUp").Parse(`interfaces:
    33  {{ range $nic, $state := . }}
    34    - name: {{ $nic }}
    35      type: ethernet
    36      state: {{ $state }}
    37  {{ end }}
    38  `)
    39  	Expect(err).ToNot(HaveOccurred())
    40  
    41  	stringBuilder := strings.Builder{}
    42  	err = tmp.Execute(&stringBuilder, states)
    43  	Expect(err).ToNot(HaveOccurred())
    44  
    45  	return nmstate.NewState(stringBuilder.String())
    46  }
    47  func ethernetNicsUp(nics ...string) nmstate.State {
    48  	states := map[string]string{}
    49  	for _, nic := range nics {
    50  		states[nic] = "up"
    51  	}
    52  	return ethernetNicsState(states)
    53  }
    54  
    55  func linuxBrUp(bridgeName string) nmstate.State {
    56  	return nmstate.NewState(fmt.Sprintf(`interfaces:
    57    - name: %s
    58      type: linux-bridge
    59      state: up
    60      bridge:
    61        port:
    62          - name: %s
    63          - name: %s
    64  `, bridgeName, firstSecondaryNic, secondSecondaryNic))
    65  }
    66  
    67  func linuxBrUpWithDefaults(bridgeName string) nmstate.State {
    68  	return nmstate.NewState(fmt.Sprintf(`interfaces:
    69    - name: %s
    70      type: linux-bridge
    71      state: up
    72      bridge:
    73        port:
    74          - name: %s
    75            vlan:
    76              mode: trunk
    77              trunk-tags:
    78              - id-range:
    79                  max: 4094
    80                  min: 2
    81          - name: %s
    82            vlan:
    83              mode: trunk
    84              trunk-tags:
    85              - id-range:
    86                  max: 4094
    87                  min: 2
    88  `, bridgeName, firstSecondaryNic, secondSecondaryNic))
    89  }
    90  
    91  func linuxBrAbsent(bridgeName string) nmstate.State {
    92  	return nmstate.NewState(fmt.Sprintf(`interfaces:
    93    - name: %s
    94      type: linux-bridge
    95      state: absent
    96  `, bridgeName))
    97  }
    98  
    99  func linuxBrUpNoPorts(bridgeName string) nmstate.State {
   100  	return nmstate.NewState(fmt.Sprintf(`interfaces:
   101    - name: %s
   102      type: linux-bridge
   103      state: up
   104      bridge:
   105        options:
   106          stp:
   107            enabled: false
   108        port: []
   109  `, bridgeName))
   110  }
   111  
   112  func linuxBrUpWithDisabledVlan(bridgeName string) nmstate.State {
   113  	return nmstate.NewState(fmt.Sprintf(`interfaces:
   114    - name: %s
   115      type: linux-bridge
   116      state: up
   117      bridge:
   118        options:
   119          stp:
   120            enabled: false
   121        port:
   122          - name: %s
   123            vlan: {}
   124          - name: %s
   125            vlan: {}
   126  `, bridgeName, firstSecondaryNic, secondSecondaryNic))
   127  }
   128  
   129  func ovsBrAbsent(bridgeName string) nmstate.State {
   130  	return nmstate.NewState(fmt.Sprintf(`interfaces:
   131    - name: %s
   132      type: ovs-bridge
   133      state: absent`, bridgeName))
   134  }
   135  
   136  func ovsBrUp(bridgeName string) nmstate.State {
   137  	return nmstate.NewState(fmt.Sprintf(`interfaces:
   138    - name: %s
   139      type: ovs-bridge
   140      state: up
   141      bridge:
   142        options:
   143          stp: false
   144        port:
   145          - name: %s
   146          - name: %s
   147  `, bridgeName, firstSecondaryNic, secondSecondaryNic))
   148  }
   149  
   150  func ovsbBrWithInternalInterface(bridgeName string) nmstate.State {
   151  	return nmstate.NewState(fmt.Sprintf(`interfaces:
   152    - name: ovs0
   153      type: ovs-interface
   154      state: up
   155      ipv4:
   156        enabled: true
   157        address:
   158          - ip: 192.0.2.1
   159            prefix-length: 24
   160    - name: %s
   161      type: ovs-bridge
   162      state: up
   163      bridge:
   164        options:
   165          stp: true
   166        port:
   167          - name: %s
   168          - name: ovs0`,
   169  		bridgeName, firstSecondaryNic))
   170  }
   171  
   172  func ifaceUpWithStaticIP(iface, ipAddress, prefixLen string) nmstate.State {
   173  	return nmstate.NewState(fmt.Sprintf(`interfaces:
   174      - name: %s
   175        type: ethernet
   176        state: up
   177        ipv4:
   178          address:
   179          - ip: %s
   180            prefix-length: %s
   181          dhcp: false
   182          enabled: true
   183  `, iface, ipAddress, prefixLen))
   184  }
   185  
   186  func ifaceUpWithStaticIPAbsent(firstSecondaryNic string) nmstate.State {
   187  	return nmstate.NewState(fmt.Sprintf(`interfaces:
   188    - name: %s
   189      type: ethernet
   190      state: up
   191      ipv4:
   192        enabled: false
   193  `, firstSecondaryNic))
   194  }
   195  
   196  func ifaceUpWithVlanUp(iface, vlanID string) nmstate.State {
   197  	return nmstate.NewState(fmt.Sprintf(`interfaces:
   198      - name: %s.%s
   199        type: vlan
   200        state: up
   201        vlan:
   202          base-iface: %s
   203          id: %s
   204  `, iface, vlanID, iface, vlanID))
   205  }
   206  
   207  func vlanAbsent(iface, vlanID string) nmstate.State {
   208  	return nmstate.NewState(fmt.Sprintf(`interfaces:
   209      - name: %s.%s
   210        type: vlan
   211        state: absent
   212        vlan:
   213          base-iface: %s
   214          id: %s
   215  `, iface, vlanID, iface, vlanID))
   216  }
   217  
   218  // vrfUp creates VRF vrfID and adds the interfaces from ifaceList to that VRF.
   219  func vrfUp(vrfID string, ifaceList ...string) nmstate.State {
   220  	ifaces := strings.Join(ifaceList, ",")
   221  	return nmstate.NewState(fmt.Sprintf(`interfaces:
   222      - name: vrf%s
   223        type: vrf
   224        state: up
   225        vrf:
   226          port: [%s]
   227          route-table-id: %[1]s
   228  `, vrfID, ifaces))
   229  }
   230  
   231  // vrfAbsent removes VRF vrfID.
   232  func vrfAbsent(vrfID string) nmstate.State {
   233  	return nmstate.NewState(fmt.Sprintf(`interfaces:
   234      - name: vrf%s
   235        type: vrf
   236        state: absent
   237  `, vrfID))
   238  }
   239  
   240  func interfaceAbsent(iface string) nmstate.State {
   241  	return nmstate.NewState(fmt.Sprintf(`interfaces:
   242      - name: %s
   243        state: absent
   244  `, iface))
   245  }
   246  
   247  func ifaceIPDisabled(iface string) nmstate.State {
   248  	return nmstate.NewState(fmt.Sprintf(`interfaces:
   249      - name: %s
   250        type: ethernet
   251        state: up
   252        ipv4:
   253          enabled: false
   254        ipv6:
   255          enabled: false
   256  `, iface))
   257  }
   258  
   259  func vlanUpWithStaticIP(iface, ipAddress string) nmstate.State {
   260  	return nmstate.NewState(fmt.Sprintf(`interfaces:
   261      - name: %s
   262        type: vlan
   263        state: up
   264        ipv4:
   265          address:
   266          - ip: %s
   267            prefix-length: 24
   268          dhcp: false
   269          enabled: true
   270  `, iface, ipAddress))
   271  }
   272  
   273  func resetPrimaryAndSecondaryNICs() nmstate.State {
   274  	return nmstate.NewState(fmt.Sprintf(`interfaces:
   275    - name: %s
   276      type: ethernet
   277      state: up
   278      ipv4:
   279        enabled: true
   280        dhcp: true
   281      ipv6:
   282        enabled: true
   283        dhcp: true
   284        autoconf: true
   285    - name: %s
   286      type: ethernet
   287      state: up
   288      ipv4:
   289        enabled: false
   290      ipv6:
   291        enabled: false
   292    - name: %s
   293      state: up
   294      type: ethernet
   295      ipv4:
   296        enabled: false
   297      ipv6:
   298        enabled: false
   299  
   300  `, primaryNic, firstSecondaryNic, secondSecondaryNic))
   301  }
   302  
   303  func bridgeOnTheSecondaryInterfaceState() nmstate.State {
   304  	return nmstate.NewState(`interfaces:
   305    - name: brext
   306      type: linux-bridge
   307      state: up
   308      ipv4: "{{ capture.secondary-iface.interfaces.0.ipv4 }}"
   309      bridge:
   310        options:
   311          stp:
   312            enabled: false
   313        port:
   314        - name: "{{ capture.secondary-iface.interfaces.0.name }}"
   315  routes:
   316    config: "{{ capture.bridge-routes.routes.running }}"
   317  `)
   318  }
   319  
   320  func matchingBond(expectedBond map[string]interface{}) types.GomegaMatcher {
   321  	expectedLinkAggregation := expectedBond["link-aggregation"].(map[string]interface{})
   322  	expectedOptions := expectedLinkAggregation["options"].(map[string]interface{})
   323  	return SatisfyAll(
   324  		HaveKeyWithValue("name", expectedBond["name"]),
   325  		HaveKeyWithValue("type", expectedBond["type"]),
   326  		HaveKeyWithValue("state", expectedBond["state"]),
   327  		HaveKeyWithValue("link-aggregation", SatisfyAll(
   328  			HaveKeyWithValue("mode", expectedLinkAggregation["mode"]),
   329  			HaveKeyWithValue(portFieldName, ConsistOf(expectedLinkAggregation[portFieldName])),
   330  			HaveKeyWithValue("options", HaveKeyWithValue("miimon", expectedOptions["miimon"])),
   331  		)),
   332  	)
   333  }