|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubeflow Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package core |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/google/go-cmp/cmp" |
| 24 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 25 | + |
| 26 | + testingutil "github.com/kubeflow/trainer/v2/pkg/util/testing" |
| 27 | +) |
| 28 | + |
| 29 | +func TestRuntimes(t *testing.T) { |
| 30 | + cases := map[string]struct { |
| 31 | + }{ |
| 32 | + "returns a copy of the runtimes map": {}, |
| 33 | + } |
| 34 | + for name := range cases { |
| 35 | + t.Run(name, func(t *testing.T) { |
| 36 | + ctx, cancel := context.WithCancel(context.Background()) |
| 37 | + t.Cleanup(cancel) |
| 38 | + clientBuilder := testingutil.NewClientBuilder() |
| 39 | + c := clientBuilder.Build() |
| 40 | + |
| 41 | + newRuntimes, err := New(ctx, c, testingutil.AsIndex(clientBuilder), nil) |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("Failed to initialize runtimes: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + gotRuntimes := Runtimes() |
| 47 | + if diff := cmp.Diff(newRuntimes, gotRuntimes, cmpopts.IgnoreUnexported(TrainingRuntime{}, ClusterTrainingRuntime{})); len(diff) != 0 { |
| 48 | + t.Errorf("Unexpected difference between new and got runtimes (-want,+got):\n%s", diff) |
| 49 | + } |
| 50 | + |
| 51 | + // Verify that modifying the returned map does not affect the persisted runtimes. |
| 52 | + gotRuntimes["mutated-key"] = nil |
| 53 | + gotRuntimes[TrainingRuntimeGroupKind] = nil |
| 54 | + delete(gotRuntimes, ClusterTrainingRuntimeGroupKind) |
| 55 | + |
| 56 | + if _, exists := runtimes["mutated-key"]; exists { |
| 57 | + t.Error("Adding a key to Runtimes() return value should not affect the persisted runtimes") |
| 58 | + } |
| 59 | + if runtimes[TrainingRuntimeGroupKind] == nil { |
| 60 | + t.Error("Modifying an existing key in Runtimes() return value should not affect the persisted runtimes") |
| 61 | + } |
| 62 | + if _, exists := runtimes[ClusterTrainingRuntimeGroupKind]; !exists { |
| 63 | + t.Error("Deleting a key from Runtimes() return value should not affect the persisted runtimes") |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
0 commit comments