Skip to content

Commit

Permalink
vai disabled/enabled tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brudnak committed Sep 5, 2024
1 parent 453e2d7 commit cb04617
Show file tree
Hide file tree
Showing 7 changed files with 1,415 additions and 0 deletions.
133 changes: 133 additions & 0 deletions tests/v2/validation/steve/vai/pod_filter_test_cases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package vai

import (
"fmt"
"net/url"

namegen "github.com/rancher/shepherd/pkg/namegenerator"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type podFilterTestCase struct {
name string
createPods func() ([]v1.Pod, []string, []string, []string)
filter func(namespaces []string) url.Values
expectFound bool
supportedWithVai bool
}

func (p podFilterTestCase) SupportedWithVai() bool {
return p.supportedWithVai
}

// Helper function to create a pod
func createPod(name, namespace, image string) v1.Pod {
return v1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace},
Spec: v1.PodSpec{
Containers: []v1.Container{{Name: image, Image: image}},
},
}
}

var podFilterTestCases = []podFilterTestCase{
{
name: "Filter by nginx image",
createPods: func() ([]v1.Pod, []string, []string, []string) {
suffix := namegen.RandStringLower(randomStringLength)
ns1 := fmt.Sprintf("namespace1-%s", suffix)
ns2 := fmt.Sprintf("namespace2-%s", suffix)
name1 := fmt.Sprintf("nginx-pod-%s", namegen.RandStringLower(randomStringLength))
name2 := fmt.Sprintf("busybox-pod-%s", namegen.RandStringLower(randomStringLength))
name3 := fmt.Sprintf("alpine-pod-%s", namegen.RandStringLower(randomStringLength))

pods := []v1.Pod{
createPod(name1, ns1, "nginx"),
createPod(name2, ns2, "busybox"),
createPod(name3, ns1, "alpine"),
}

expectedNames := []string{name1}
allNamespaces := []string{ns1, ns2}
expectedNamespaces := []string{ns1, ns2}

return pods, expectedNames, allNamespaces, expectedNamespaces
},
filter: func(namespaces []string) url.Values {
return url.Values{
"filter": []string{"spec.containers.image=nginx"},
"projectsornamespaces": namespaces,
}
},
expectFound: true,
supportedWithVai: true,
},
{
name: "Filter by busybox image",
createPods: func() ([]v1.Pod, []string, []string, []string) {
suffix := namegen.RandStringLower(randomStringLength)
ns1 := fmt.Sprintf("namespace1-%s", suffix)
ns2 := fmt.Sprintf("namespace2-%s", suffix)
name1 := fmt.Sprintf("nginx-pod-%s", namegen.RandStringLower(randomStringLength))
name2 := fmt.Sprintf("busybox-pod-%s", namegen.RandStringLower(randomStringLength))
name3 := fmt.Sprintf("alpine-pod-%s", namegen.RandStringLower(randomStringLength))

pods := []v1.Pod{
createPod(name1, ns1, "nginx"),
createPod(name2, ns2, "busybox"),
createPod(name3, ns1, "alpine"),
}

expectedNames := []string{name2}
allNamespaces := []string{ns1, ns2}
expectedNamespaces := []string{ns2}

return pods, expectedNames, allNamespaces, expectedNamespaces
},
filter: func(namespaces []string) url.Values {
return url.Values{
"filter": []string{"spec.containers.image=busybox"},
"projectsornamespaces": namespaces,
}
},
expectFound: true,
supportedWithVai: true,
},
{
name: "Filter by non-existent image",
createPods: func() ([]v1.Pod, []string, []string, []string) {
suffix := namegen.RandStringLower(randomStringLength)
ns1 := fmt.Sprintf("namespace1-%s", suffix)
ns2 := fmt.Sprintf("namespace2-%s", suffix)
name1 := fmt.Sprintf("nginx-pod-%s", namegen.RandStringLower(randomStringLength))
name2 := fmt.Sprintf("busybox-pod-%s", namegen.RandStringLower(randomStringLength))
name3 := fmt.Sprintf("alpine-pod-%s", namegen.RandStringLower(randomStringLength))

pods := []v1.Pod{
createPod(name1, ns1, "nginx"),
createPod(name2, ns2, "busybox"),
createPod(name3, ns1, "alpine"),
}

// Add NodeName to pods
pods[0].Spec.NodeName = "node1"
pods[1].Spec.NodeName = "node2"
pods[2].Spec.NodeName = "node1"

expectedNames := []string{}
allNamespaces := []string{ns1, ns2}
expectedNamespaces := []string{ns1, ns2}

return pods, expectedNames, allNamespaces, expectedNamespaces
},
filter: func(namespaces []string) url.Values {
return url.Values{
"filter": []string{"spec.containers.image=redis"},
"projectsornamespaces": namespaces,
}
},
expectFound: false,
supportedWithVai: true,
},
}
102 changes: 102 additions & 0 deletions tests/v2/validation/steve/vai/scripts/script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/sh
set -e

echo "Starting script execution..."

# Function to check if Go is installed and working
check_go() {
if /usr/local/go/bin/go version >/dev/null 2>&1; then
return 0
fi
return 1
}

# Install Go if not already installed
if ! check_go; then
echo "Go not found. Installing Go..."
curl -L -o go1.22.4.linux-amd64.tar.gz https://go.dev/dl/go1.22.4.linux-amd64.tar.gz --insecure
tar -C /usr/local -xzf go1.22.4.linux-amd64.tar.gz
rm go1.22.4.linux-amd64.tar.gz
echo "Go installed successfully."
else
echo "Go is already installed."
fi

# Always set the PATH to include Go
export PATH=$PATH:/usr/local/go/bin

echo "Checking Go version:"
go version

# Check if vai-query already exists
if [ ! -f /usr/local/bin/vai-query ]; then
echo "vai-query not found. Building vai-query program..."
mkdir -p /root/vai-query
cd /root/vai-query

# Initialize Go module if it doesn't exist
if [ ! -f go.mod ]; then
go mod init vai-query
fi

# Create or update main.go
cat << EOF > main.go
package main
import (
"database/sql"
"fmt"
"log"
"os"
"strings"
"github.com/pkg/errors"
_ "modernc.org/sqlite"
)
func main() {
tableName := strings.ReplaceAll(os.Getenv("TABLE_NAME"), "\"", "")
resourceName := os.Getenv("RESOURCE_NAME")
db, err := sql.Open("sqlite", "/var/lib/rancher/informer_object_fields.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
query := fmt.Sprintf("SELECT \"metadata.name\" FROM \"%s\" WHERE \"metadata.name\" = ?", tableName)
stmt, err := db.Prepare(query)
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
var result string
err = stmt.QueryRow(resourceName).Scan(&result)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
fmt.Println("Resource not found")
} else {
log.Fatal(err)
}
} else {
fmt.Println("Found resource:", result)
}
}
EOF

# Get dependencies
go get github.com/pkg/errors
go get modernc.org/sqlite

# Build the program
go build -o /usr/local/bin/vai-query main.go
echo "Pure Go vai-query program built successfully."
else
echo "vai-query program already exists. Using existing binary."
fi

echo "Executing the query program..."
TABLE_NAME="${TABLE_NAME}" RESOURCE_NAME="${RESOURCE_NAME}" /usr/local/bin/vai-query

echo "Script execution completed."
Loading

0 comments on commit cb04617

Please sign in to comment.