Skip to content

Commit

Permalink
fix(v1beta3): ensure resources ID is copied (#73)
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian authored Aug 24, 2023
1 parent 7a6ffcb commit f96c282
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions go/node/types/v1beta3/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (m Resources) Validate() error {

func (m Resources) Dup() Resources {
res := Resources{
ID: m.ID,
CPU: m.CPU.Dup(),
GPU: m.GPU.Dup(),
Memory: m.Memory.Dup(),
Expand Down
105 changes: 105 additions & 0 deletions go/node/types/v1beta3/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,108 @@ func TestVolumes_Equal(t *testing.T) {
dVolumes[0].Name = "class2"
require.False(t, volumes.Equal(dVolumes))
}

func TestResources_ValidateID(t *testing.T) {
res := Resources{}

err := res.Validate()
require.ErrorContains(t, err, "resources ID must be > 0")
}

func TestResources_ValidateCPU(t *testing.T) {
res := Resources{
ID: 1,
}

err := res.Validate()
require.ErrorContains(t, err, "CPU must not be nil")
}

func TestResources_ValidateGPU(t *testing.T) {
res := Resources{
ID: 1,
CPU: &CPU{},
}

err := res.Validate()
require.ErrorContains(t, err, "GPU must not be nil")
}

func TestResources_ValidateMemory(t *testing.T) {
res := Resources{
ID: 1,
CPU: &CPU{},
GPU: &GPU{},
}

err := res.Validate()
require.ErrorContains(t, err, "memory must not be nil")
}

func TestResources_ValidateStorage(t *testing.T) {
res := Resources{
ID: 1,
CPU: &CPU{},
GPU: &GPU{},
Memory: &Memory{},
}

err := res.Validate()
require.ErrorContains(t, err, "storage must not be nil")
}

func TestResources_ValidateEndpoints(t *testing.T) {
res := Resources{
ID: 1,
CPU: &CPU{},
GPU: &GPU{},
Memory: &Memory{},
Storage: make(Volumes, 0),
}

err := res.Validate()
require.ErrorContains(t, err, "endpoints must not be nil")
}

func TestResources_Validate(t *testing.T) {
res := Resources{
ID: 1,
CPU: &CPU{},
GPU: &GPU{},
Memory: &Memory{},
Storage: make(Volumes, 0),
Endpoints: make(Endpoints, 0),
}

err := res.Validate()
require.NoError(t, err)
}

func TestResources_DupInvalidID(t *testing.T) {
res := Resources{
CPU: &CPU{},
GPU: &GPU{},
Memory: &Memory{},
Storage: make(Volumes, 0),
Endpoints: make(Endpoints, 0),
}

dup := res.Dup()
err := dup.Validate()
require.ErrorContains(t, err, "resources ID must be > 0")
}

func TestResources_DupValid(t *testing.T) {
res := Resources{
ID: 1,
CPU: &CPU{},
GPU: &GPU{},
Memory: &Memory{},
Storage: make(Volumes, 0),
Endpoints: make(Endpoints, 0),
}

dup := res.Dup()
err := dup.Validate()
require.NoError(t, err)
}

0 comments on commit f96c282

Please sign in to comment.