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

Add Unit Tests for Forest Quota Management Plugin Library #509

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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,
Expand All @@ -18,7 +18,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"

"github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/quotaplugins/quota-forest/quota-manager/quota"
Expand Down Expand Up @@ -52,7 +51,7 @@ func main() {
for _, treeName := range treeNames {
fName := prefix + treeName + ".json"
fmt.Printf("Tree file name: %s\n", fName)
jsonTree, err := ioutil.ReadFile(fName)
jsonTree, err := os.ReadFile(fName)
if err != nil {
fmt.Printf("error reading quota tree file: %s", fName)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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,
Expand All @@ -18,7 +18,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"

"github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/quotaplugins/quota-forest/quota-manager/quota"
Expand Down Expand Up @@ -52,7 +51,7 @@ func main() {
fmt.Println(quotaManager.GetModeString())

// add a quota tree from file
jsonTree, err := ioutil.ReadFile(treeFileName)
jsonTree, err := os.ReadFile(treeFileName)
if err != nil {
fmt.Printf("error reading quota tree file: %s", treeFileName)
return
Expand Down
5 changes: 2 additions & 3 deletions pkg/quotaplugins/quota-forest/quota-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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,
Expand All @@ -18,7 +18,6 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"

"github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/quotaplugins/quota-forest/quota-manager/quota"
Expand All @@ -40,7 +39,7 @@ func main() {
fmt.Println("==> Creating Quota Manager")
fmt.Println("**************************")
quotaManager := quota.NewManager()
treeJsonString, err := ioutil.ReadFile(treeFileName)
treeJsonString, err := os.ReadFile(treeFileName)
if err != nil {
fmt.Printf("error reading quota tree file: %s", treeFileName)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,76 @@ func TestAllocation_SetValue(t *testing.T) {
}
}

func TestAllocation_Add(t *testing.T) {
type fields struct {
value1 []int
value2 []int
}
type args struct {
other *Allocation
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "test1",
fields: fields{
value1: []int{1, 2},
value2: []int{4, 6},
},
args: args{
other: &Allocation{
x: []int{3, 4},
},
},
want: true,
},
{
name: "test2",
fields: fields{
value1: []int{1, 2},
value2: []int{1, 2},
},
args: args{
other: &Allocation{
x: []int{3, 4, 5},
},
},
want: false,
},
{
name: "test3",
fields: fields{
value1: []int{1, 2},
value2: []int{4, 9},
},
args: args{
other: &Allocation{
x: []int{3, 4},
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &Allocation{
x: tt.fields.value1,
}
if got := a.Add(tt.args.other) && reflect.DeepEqual(a.x, tt.fields.value2); got != tt.want {
t.Errorf("Allocation.Add() = %v, want %v; result = %v, want %v",
got, tt.want, a.x, tt.fields.value2)
}
})
}
}

func TestAllocation_Fit(t *testing.T) {
type fields struct {
x []int
value []int
}
type args struct {
allocated *Allocation
Expand All @@ -201,7 +268,7 @@ func TestAllocation_Fit(t *testing.T) {
}{
{name: "test1",
fields: fields{
x: []int{1, 2, 3},
value: []int{1, 2, 3},
},
args: args{
allocated: &Allocation{
Expand All @@ -215,7 +282,7 @@ func TestAllocation_Fit(t *testing.T) {
},
{name: "test2",
fields: fields{
x: []int{1, 2, 3},
value: []int{1, 2, 3},
},
args: args{
allocated: &Allocation{
Expand All @@ -229,7 +296,7 @@ func TestAllocation_Fit(t *testing.T) {
},
{name: "test3",
fields: fields{
x: []int{1, 2, 3},
value: []int{1, 2, 3},
},
args: args{
allocated: &Allocation{
Expand All @@ -243,7 +310,7 @@ func TestAllocation_Fit(t *testing.T) {
},
{name: "test4",
fields: fields{
x: []int{1, 2, 3},
value: []int{1, 2, 3},
},
args: args{
allocated: &Allocation{
Expand All @@ -257,7 +324,7 @@ func TestAllocation_Fit(t *testing.T) {
},
{name: "test5",
fields: fields{
x: []int{1, 2, 3},
value: []int{1, 2, 3},
},
args: args{
allocated: &Allocation{
Expand All @@ -273,7 +340,7 @@ func TestAllocation_Fit(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &Allocation{
x: tt.fields.x,
x: tt.fields.value,
}
if got := a.Fit(tt.args.allocated, tt.args.capacity); got != tt.want {
t.Errorf("Allocation.Fit() = %v, want %v", got, tt.want)
Expand All @@ -282,9 +349,125 @@ func TestAllocation_Fit(t *testing.T) {
}
}

func TestAllocation_IsZero(t *testing.T) {
type fields struct {
value []int
}
tests := []struct {
name string
fields fields
want bool
}{
{
name: "test1",
fields: fields{
value: []int{0, 0, 0},
},
want: true,
},
{
name: "test2",
fields: fields{
value: []int{0, 1},
},
want: false,
},
{
name: "test3",
fields: fields{
value: []int{1, 2, -4},
},
want: false,
},
{
name: "test4",
fields: fields{
value: []int{},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &Allocation{
x: tt.fields.value,
}
if got := a.IsZero(); got != tt.want {
t.Errorf("Allocation.IsZero() = %v, want %v", got, tt.want)
}
})
}
}

func TestAllocation_Equal(t *testing.T) {
type fields struct {
value []int
}
type args struct {
other *Allocation
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "test1",
fields: fields{
value: []int{1, 2, 3},
},
args: args{
other: &Allocation{x: []int{1, 2, 3}},
},
want: true,
},
{
name: "test2",
fields: fields{
value: []int{},
},
args: args{
other: &Allocation{x: []int{}},
},
want: true,
},
{
name: "test3",
fields: fields{
value: []int{1, 2, 3},
},
args: args{
other: &Allocation{x: []int{4, 5, 6}},
},
want: false,
},
{
name: "test4",
fields: fields{
value: []int{1, 2},
},
args: args{
other: &Allocation{x: []int{1, 2, 3}},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &Allocation{
x: tt.fields.value,
}
if got := a.Equal(tt.args.other); got != tt.want {
t.Errorf("Allocation.Equal() = %v, want %v", got, tt.want)
}
})
}
}

func TestAllocation_StringPretty(t *testing.T) {
type fields struct {
x []int
value []int
}
type args struct {
resourceNames []string
Expand All @@ -298,7 +481,7 @@ func TestAllocation_StringPretty(t *testing.T) {
{
name: "test1",
fields: fields{
x: []int{1, 2, 3},
value: []int{1, 2, 3},
},
args: args{
resourceNames: []string{"cpu", "memory", "gpu"},
Expand All @@ -308,7 +491,7 @@ func TestAllocation_StringPretty(t *testing.T) {
{
name: "test2",
fields: fields{
x: []int{1, 2, 3},
value: []int{1, 2, 3},
},
args: args{
resourceNames: []string{"cpu", "memory"},
Expand All @@ -320,7 +503,7 @@ func TestAllocation_StringPretty(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &Allocation{
x: tt.fields.x,
x: tt.fields.value,
}
if got := a.StringPretty(tt.args.resourceNames); got != tt.want {
t.Errorf("Allocation.StringPretty() = %v, want %v", got, tt.want)
Expand Down
Loading