Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

website for our cnf #1544

Merged
merged 39 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
3bd5d2c
website for our cnf
aabughosh Oct 19, 2023
2bfd874
a few changes(add upload file) and added the -s for server
aabughosh Oct 23, 2023
1f166cd
change folder name
aabughosh Oct 23, 2023
b628a53
Delete index.html
aabughosh Oct 23, 2023
64198ab
Merge branch 'main' into cnfweb
aabughosh Oct 25, 2023
bd8cba1
added new function and rename names
aabughosh Oct 25, 2023
b4a4752
Merge branch 'main' into cnfweb
aabughosh Oct 25, 2023
7da80cd
Merge branch 'main' into cnfweb
aabughosh Oct 26, 2023
f82548b
add new file for html function
aabughosh Oct 26, 2023
503b59f
Merge branch 'main' into cnfweb
aabughosh Oct 30, 2023
1c3eaa7
fix for saving the log to be in a bif and not in a file
aabughosh Oct 31, 2023
c65f25a
add bufer implemntation fir logs
aabughosh Nov 2, 2023
0d4708b
Merge branch 'main' into cnfweb
aabughosh Nov 2, 2023
f36b166
update the flag
aabughosh Nov 2, 2023
8332a1e
Merge branch 'cnfweb' of https://github.com/test-network-function/cnf…
aabughosh Nov 2, 2023
0d1deba
few changes for linter
aabughosh Nov 2, 2023
de14d59
Update run-cnf-suites.sh
aabughosh Nov 2, 2023
154f481
udpate typos
aabughosh Nov 2, 2023
d93a963
Merge branch 'main' into cnfweb
aabughosh Nov 6, 2023
0add114
Merge branch 'main' into cnfweb
aabughosh Nov 7, 2023
8e4ae74
david comments
aabughosh Nov 7, 2023
adb89ca
Merge branch 'cnfweb' of https://github.com/test-network-function/cnf…
aabughosh Nov 7, 2023
2fbd6a7
update the flag
aabughosh Nov 7, 2023
e3818a0
Merge branch 'main' into cnfweb
aabughosh Nov 7, 2023
d4524a8
website for our cnf
aabughosh Oct 19, 2023
d4dc99f
a few changes(add upload file) and added the -s for server
aabughosh Oct 23, 2023
2fd48a1
change folder name
aabughosh Oct 23, 2023
78935e5
Delete index.html
aabughosh Oct 23, 2023
e7e0a8f
added new function and rename names
aabughosh Oct 25, 2023
1175fd3
add new file for html function
aabughosh Oct 26, 2023
e85767a
fix for saving the log to be in a bif and not in a file
aabughosh Oct 31, 2023
9827cef
add bufer implemntation fir logs
aabughosh Nov 2, 2023
7ce8b09
update the flag
aabughosh Nov 2, 2023
f6fcba6
few changes for linter
aabughosh Nov 2, 2023
2bcec7d
Update run-cnf-suites.sh
aabughosh Nov 2, 2023
f10d536
udpate typos
aabughosh Nov 2, 2023
ab9c71d
david comments
aabughosh Nov 7, 2023
93a3238
update the flag
aabughosh Nov 7, 2023
06e8041
Merge branch 'cnfweb' of https://github.com/test-network-function/cnf…
aabughosh Nov 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 198 additions & 21 deletions cnf-certification-test/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@

import (
_ "embed"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"

"github.com/onsi/ginkgo/v2"
"github.com/sirupsen/logrus"

Check failure on line 33 in cnf-certification-test/suite_test.go

View workflow job for this annotation

GitHub Actions / Run Linters and Vet

dupImport: package is imported 2 times under different aliases on lines 33 and 34 (gocritic)
log "github.com/sirupsen/logrus"

Check failure on line 34 in cnf-certification-test/suite_test.go

View workflow job for this annotation

GitHub Actions / Run Linters and Vet

dupImport: package is imported 2 times under different aliases on lines 33 and 34 (gocritic)
"github.com/test-network-function/cnf-certification-test/cnf-certification-test/results"
"github.com/test-network-function/cnf-certification-test/pkg/claimhelper"
"github.com/test-network-function/cnf-certification-test/pkg/collector"
Expand Down Expand Up @@ -56,8 +62,10 @@
defaultClaimPath = ".."
defaultCliArgValue = ""
junitFlagKey = "junit"
serverModeFlag = "server-mode"
TNFReportKey = "cnf-certification-test"
extraInfoKey = "testsExtraInfo"
defaultServerMode = "false"
aabughosh marked this conversation as resolved.
Show resolved Hide resolved
)

var (
Expand All @@ -77,13 +85,16 @@
// ClaimFormat is the current version for the claim file format to be produced by the TNF test suite.
// A client decoding this claim file must support decoding its specific version.
ClaimFormatVersion string
serverMode *string
)

func init() {
claimPath = flag.String(claimPathFlagKey, defaultClaimPath,
"the path where the claimfile will be output")
junitPath = flag.String(junitFlagKey, defaultCliArgValue,
"the path for the junit format report")
serverMode = flag.String(serverModeFlag, defaultServerMode,
"run test with webserver")
}

// setLogLevel sets the log level for logrus based on the "TNF_LOG_LEVEL" environment variable
Expand Down Expand Up @@ -126,8 +137,7 @@
return gitDisplayRelease + " ( " + GitCommit + " )"
}

// TestTest invokes the CNF Certification Test Suite.
func TestTest(t *testing.T) {
func PreRun(t *testing.T) (claimData *claim.Claim, claimRoot *claim.Root) {
// When running unit tests, skip the suite
if os.Getenv("UNIT_TEST") != "" {
t.Skip("Skipping test suite when running unit tests")
Expand All @@ -147,20 +157,13 @@
log.Infof("Claim Format Version: %s", ClaimFormatVersion)
log.Infof("Ginkgo Version : %v", ginkgo.GINKGO_VERSION)
log.Infof("Labels filter : %v", ginkgoConfig.LabelFilter)

// Diagnostic functions will run when no labels are provided.
var diagnosticMode bool
if ginkgoConfig.LabelFilter == "" {
log.Infof("TNF will run in diagnostic mode so no test case will be launched.")
diagnosticMode = true
}

log.Infof("run test with webserver : %v", *serverMode)
// Set clientsholder singleton with the filenames from the env vars.
_ = clientsholder.GetClientsHolder(getK8sClientsConfigFileNames()...)

// Initialize the claim with the start time, tnf version, etc.
claimRoot := claimhelper.CreateClaimRoot()
claimData := claimRoot.Claim
claimRoot = claimhelper.CreateClaimRoot()
claimData = claimRoot.Claim
claimData.Configurations = make(map[string]interface{})
claimData.Nodes = make(map[string]interface{})
incorporateVersions(claimData)
Expand All @@ -173,17 +176,42 @@

claimData.Nodes = claimhelper.GenerateNodes()
claimhelper.UnmarshalConfigurations(configurations, claimData.Configurations)
return claimData, claimRoot
}

// initialize abort flag
testhelper.AbortTrigger = ""
// TestTest invokes the CNF Certification Test Suite.
func TestTest(t *testing.T) {

// Run tests specs only if not in diagnostic mode, otherwise all TSs would run.
var env provider.TestEnvironment
if !diagnosticMode {
env.SetNeedsRefresh()
env = provider.GetTestEnvironment()
ginkgo.RunSpecs(t, CnfCertificationTestSuiteName)
// Keep the main program running
ginkgoConfig, _ := ginkgo.GinkgoConfiguration()
if *serverMode == "false" {
claimData, claimRoot := PreRun(t)
var diagnosticMode bool
// Diagnostic functions will run when no labels are provided.

if ginkgoConfig.LabelFilter == "" {
log.Infof("TNF will run in diagnostic mode so no test case will be launched.")
diagnosticMode = true
}

// initialize abort flag
testhelper.AbortTrigger = ""

// Run tests specs only if not in diagnostic mode, otherwise all TSs would run.
var env provider.TestEnvironment
if !diagnosticMode {
env.SetNeedsRefresh()
env = provider.GetTestEnvironment()
ginkgo.RunSpecs(t, CnfCertificationTestSuiteName)
}
ContinueRun(t, diagnosticMode, env, claimData, claimRoot)
} else {
go StartServer()
select {}
}
}

func ContinueRun(t *testing.T, diagnosticMode bool, env provider.TestEnvironment, claimData *claim.Claim, claimRoot *claim.Root) {

Check failure on line 214 in cnf-certification-test/suite_test.go

View workflow job for this annotation

GitHub Actions / Run Linters and Vet

hugeParam: env is heavy (1280 bytes); consider passing it by pointer (gocritic)

endTime := time.Now()
claimData.Metadata.EndTime = endTime.UTC().Format(claimhelper.DateTimeFormatDirective)
Expand Down Expand Up @@ -211,7 +239,7 @@

// Send claim file to the collector if specified by env var
if configuration.GetTestParameters().EnableDataCollection {
err = collector.SendClaimFileToCollector(env.CollectorAppEndPoint, claimOutputFile, env.ExecutedBy, env.PartnerName, env.CollectorAppPassword)
err := collector.SendClaimFileToCollector(env.CollectorAppEndPoint, claimOutputFile, env.ExecutedBy, env.PartnerName, env.CollectorAppPassword)
if err != nil {
log.Errorf("Failed to send post request to the collector: %v", err)
}
Expand Down Expand Up @@ -264,3 +292,152 @@
ClaimFormat: ClaimFormatVersion,
}
}
func StartServer() {

HandlereqFunc()

http.HandleFunc("/runFunction", RunHandler)

fmt.Println("Server is running on :8080...")
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}

// Define an HTTP handler that triggers Ginkgo tests
func RunHandler(w http.ResponseWriter, r *http.Request) {
// Create or open a log file
filename := "log.log"
if _, err := os.Stat(filename); err == nil {
// If it exists, truncate it to remove the contents
file, err := os.OpenFile(filename, os.O_TRUNC, 0)
if err != nil {
// Handle the error if necessary
panic(err)
}
file.Close()
}
logFile, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)

Check failure on line 320 in cnf-certification-test/suite_test.go

View workflow job for this annotation

GitHub Actions / Run Linters and Vet

octalLiteral: use new octal literal style, 0o644 (gocritic)
if err != nil {
panic(err)
}
logrus.SetOutput(logFile)
originalStdout := os.Stdout
os.Stdout = logFile
// Read the YAML file
defer func() {
os.Stdout = originalStdout
logFile.Close()
}()

jsonData := r.FormValue("jsonData") // "jsonData" is the name of the JSON input field
logrus.Info(jsonData)
var data RequstedData
if err := json.Unmarshal([]byte(jsonData), &data); err != nil {
fmt.Println("Error:", err)
}
var flattenedOptions []string
flattenedOptions = flattenData(data.SelectedOptions, flattenedOptions)

// Get the file from the request
file, handler, err := r.FormFile("kubeConfigPath") // "fileInput" is the name of the file input field
if err != nil {
http.Error(w, "Unable to retrieve file from form", http.StatusBadRequest)
return
}
defer file.Close()

// Create a new file on the server to store the uploaded content
uploadedFile, err := os.Create(handler.Filename)
if err != nil {
http.Error(w, "Unable to create file for writing", http.StatusInternalServerError)
return
}
defer uploadedFile.Close()

// Copy the uploaded file's content to the new file
_, err = io.Copy(uploadedFile, file)
if err != nil {
http.Error(w, "Unable to copy file", http.StatusInternalServerError)
return
}

// Copy the uploaded file to the server file

os.Setenv("KUBECONFIG", handler.Filename)
logrus.Infof("KUBECONFIG : %v", handler.Filename)

logrus.Infof("Labels filter : %v", flattenedOptions)

// Set the output of the logger to the log file

/*data, err := os.ReadFile("tnf_config.yml")

Check failure on line 374 in cnf-certification-test/suite_test.go

View workflow job for this annotation

GitHub Actions / Run Linters and Vet

commentedOutCode: may want to remove commented-out code (gocritic)
if err != nil {
log.Fatalf("Error reading YAML file: %v", err)
}

// Unmarshal the YAML data into a Config struct
var config configuration.TestConfiguration

err = yaml.Unmarshal(data, &config)
if err != nil {
log.Fatalf("Error unmarshaling YAML: %v", err)
aabughosh marked this conversation as resolved.
Show resolved Hide resolved
}

// Modify the configuration
var namespace []configuration.Namespace
namespace = append(namespace, configuration.Namespace{Name: requestData.Field3})

config.TargetNameSpaces = namespace // Change the port to a new value

// Serialize the modified config back to YAML format
newData, err := yaml.Marshal(&config)
if err != nil {
log.Fatalf("Error marshaling YAML: %v", err)
}

// Write the modified YAML data back to the file
err = os.WriteFile("tnf_config.yml", newData, os.ModePerm)
if err != nil {
log.Fatalf("Error writing YAML file: %v", err)
}
*/
t := testing.T{}
claimData, claimRoot := PreRun(&t)
var env provider.TestEnvironment
env.SetNeedsRefresh()
env = provider.GetTestEnvironment()
// fetch the current config
suiteConfig, reporterConfig := ginkgo.GinkgoConfiguration()
// adjust it
suiteConfig.SkipStrings = []string{"NEVER-RUN"}
reporterConfig.FullTrace = true
reporterConfig.JUnitReport = "cnf-certification-tests_junit.xml"
// pass it in to RunSpecs
suiteConfig.LabelFilter = strings.Join(flattenedOptions, "")
ginkgo.RunSpecs(&t, CnfCertificationTestSuiteName, suiteConfig, reporterConfig)

ContinueRun(&t, false, env, claimData, claimRoot)
// Return the result as JSON
response := struct {
Message string `json:"Message"`
}{
Message: fmt.Sprintf("Sucsses to run %s", strings.Join(flattenedOptions, "")),
}
// Serialize the response data to JSON
jsonResponse, err := json.Marshal(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Set the Content-Type header to specify that the response is JSON
w.Header().Set("Content-Type", "application/json")

// Write the JSON response to the client
_, err = w.Write(jsonResponse)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

}
Loading
Loading