Add API support for network chain intent
[multicloud/k8s.git] / src / ncm / api / chainhandler_test.go
1 /*
2  * Copyright 2020 Intel Corporation, Inc
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package api
18
19 import (
20         "testing"
21 )
22
23 func TestIsValidNetworkChain(t *testing.T) {
24         t.Run("Valid Chains", func(t *testing.T) {
25                 validchains := []string{
26                         "app=abc,net1,app=xyz",
27                         "app=abc, net1, app=xyz",
28                         " app=abc , net1 , app=xyz ",
29                         "app.kubernets.io/name=abc,net1,app.kubernets.io/name=xyz",
30                         "app.kubernets.io/name=abc,net1,app.kubernets.io/name=xyz, net2, anotherlabel=wex",
31                 }
32                 for _, chain := range validchains {
33                         err := validateNetworkChain(chain)
34                         if err != nil {
35                                 t.Errorf("Valid network chain failed to pass: %v %v", chain, err)
36                         }
37                 }
38         })
39
40         t.Run("Invalid Chains", func(t *testing.T) {
41                 invalidchains := []string{
42                         "",
43                         "app=abc,net1,app= xyz",
44                         "app=abc,net1,xyz",
45                         "app=abc,net1",
46                         "app.kubernets.io/name=abc,net1,=xyz",
47                         "abcdefg",
48                 }
49                 for _, chain := range invalidchains {
50                         err := validateNetworkChain(chain)
51                         if err == nil {
52                                 t.Errorf("Invalid network chain passed: %v", chain)
53                         }
54                 }
55         })
56 }