-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yun-Tang Hsu <hsuy@vmware.com>
- Loading branch information
Yun-Tang Hsu
committed
Sep 27, 2022
1 parent
c9cab76
commit 153764b
Showing
13 changed files
with
1,047 additions
and
545 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
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,89 @@ | ||
// Copyright 2022 Antrea 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. | ||
|
||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
"k8s.io/client-go/kubernetes" | ||
restclient "k8s.io/client-go/rest" | ||
|
||
"antrea.io/theia/pkg/theia/portforwarder" | ||
) | ||
|
||
func TestPolicyRecommendationDelete(t *testing.T) { | ||
sparkAppID := "e292395c-3de1-11ed-b878-0242ac120002" | ||
testCases := []struct { | ||
name string | ||
testServer *httptest.Server | ||
expectedErrorMsg string | ||
}{ | ||
{ | ||
name: "valid case", | ||
testServer: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
switch strings.TrimSpace(r.URL.Path) { | ||
case fmt.Sprintf("/apis/intelligence.theia.antrea.io/v1alpha1/networkpolicyrecommendations/pr-%s", sparkAppID): | ||
if r.Method == "DELETE" { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
} else { | ||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) | ||
} | ||
} | ||
})), | ||
expectedErrorMsg: "", | ||
}, | ||
{ | ||
name: "SparkApplication not found", | ||
testServer: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
switch strings.TrimSpace(r.URL.Path) { | ||
case fmt.Sprintf("/apis/intelligence.theia.antrea.io/v1alpha1/networkpolicyrecommendations/pr-%s", sparkAppID): | ||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) | ||
} | ||
})), | ||
expectedErrorMsg: "error when deleting policy recommendation job", | ||
}, | ||
} | ||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
defer tt.testServer.Close() | ||
oldFunc := SetupTheiaClientAndConnection | ||
SetupTheiaClientAndConnection = func(cmd *cobra.Command, useClusterIP bool) (restclient.Interface, *portforwarder.PortForwarder, error) { | ||
clientConfig := &restclient.Config{Host: tt.testServer.URL, TLSClientConfig: restclient.TLSClientConfig{Insecure: true}} | ||
clientset, _ := kubernetes.NewForConfig(clientConfig) | ||
return clientset.CoreV1().RESTClient(), nil, nil | ||
} | ||
defer func() { | ||
SetupTheiaClientAndConnection = oldFunc | ||
}() | ||
cmd := new(cobra.Command) | ||
cmd.Flags().String("id", sparkAppID, "") | ||
cmd.Flags().Bool("use-cluster-ip", true, "") | ||
err := policyRecommendationDelete(cmd, []string{}) | ||
if tt.expectedErrorMsg == "" { | ||
assert.NoError(t, err) | ||
} else { | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), tt.expectedErrorMsg) | ||
} | ||
}) | ||
} | ||
} |
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,107 @@ | ||
// Copyright 2022 Antrea 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. | ||
|
||
package commands | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
"k8s.io/client-go/kubernetes" | ||
restclient "k8s.io/client-go/rest" | ||
|
||
intelligence "antrea.io/theia/pkg/apis/intelligence/v1alpha1" | ||
"antrea.io/theia/pkg/theia/portforwarder" | ||
) | ||
|
||
func TestPolicyRecommendationList(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
testServer *httptest.Server | ||
expectedMsg []string | ||
expectedErrorMsg string | ||
}{ | ||
{ | ||
name: "valid case", | ||
testServer: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
switch strings.TrimSpace(r.URL.Path) { | ||
case fmt.Sprintf("/apis/intelligence.theia.antrea.io/v1alpha1/networkpolicyrecommendations"): | ||
nprList := &intelligence.NetworkPolicyRecommendationList{ | ||
Items: []intelligence.NetworkPolicyRecommendation{ | ||
{Status: intelligence.NetworkPolicyRecommendationStatus{ | ||
SparkApplication: "test1", | ||
}}, | ||
}, | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
json.NewEncoder(w).Encode(nprList) | ||
} | ||
})), | ||
expectedMsg: []string{"test1"}, | ||
expectedErrorMsg: "", | ||
}, | ||
{ | ||
name: "NetworkPolicyRecommendationList not found", | ||
testServer: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
switch strings.TrimSpace(r.URL.Path) { | ||
case fmt.Sprintf("/apis/intelligence.theia.antrea.io/v1alpha1/networkpolicyrecommendations"): | ||
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) | ||
} | ||
})), | ||
expectedMsg: []string{}, | ||
expectedErrorMsg: "error when getting policy recommendation jobs from Theia Manager", | ||
}, | ||
} | ||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
defer tt.testServer.Close() | ||
oldFunc := SetupTheiaClientAndConnection | ||
SetupTheiaClientAndConnection = func(cmd *cobra.Command, useClusterIP bool) (restclient.Interface, *portforwarder.PortForwarder, error) { | ||
clientConfig := &restclient.Config{Host: tt.testServer.URL, TLSClientConfig: restclient.TLSClientConfig{Insecure: true}} | ||
clientset, _ := kubernetes.NewForConfig(clientConfig) | ||
return clientset.CoreV1().RESTClient(), nil, nil | ||
} | ||
defer func() { | ||
SetupTheiaClientAndConnection = oldFunc | ||
}() | ||
cmd := new(cobra.Command) | ||
cmd.Flags().Bool("use-cluster-ip", true, "") | ||
|
||
orig := os.Stdout | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
err := policyRecommendationList(cmd, []string{}) | ||
if tt.expectedErrorMsg == "" { | ||
assert.NoError(t, err) | ||
outcome := readStdout(t, r, w) | ||
os.Stdout = orig | ||
assert.Contains(t, outcome, "test1") | ||
for _, msg := range tt.expectedMsg { | ||
assert.Contains(t, outcome, msg) | ||
} | ||
} else { | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), tt.expectedErrorMsg) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.