|
| 1 | +/* |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package build |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "testing" |
| 23 | + |
| 24 | + v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1" |
| 25 | + "github.com/apache/camel-k/v2/pkg/internal" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 29 | + "k8s.io/apimachinery/pkg/types" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 31 | +) |
| 32 | + |
| 33 | +func TestReconcileBuild(t *testing.T) { |
| 34 | + ctx := context.TODO() |
| 35 | + |
| 36 | + build := &v1.Build{ |
| 37 | + ObjectMeta: metav1.ObjectMeta{ |
| 38 | + Name: "test-build", |
| 39 | + Namespace: "default", |
| 40 | + }, |
| 41 | + Spec: v1.BuildSpec{ |
| 42 | + Tasks: []v1.Task{ |
| 43 | + { |
| 44 | + Builder: &v1.BuilderTask{ |
| 45 | + BaseTask: v1.BaseTask{ |
| 46 | + Name: "builder", |
| 47 | + Configuration: v1.BuildConfiguration{ |
| 48 | + Strategy: v1.BuildStrategyRoutine, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + c, err := internal.NewFakeClient(build) |
| 58 | + require.NoError(t, err) |
| 59 | + |
| 60 | + r := &reconcileBuild{ |
| 61 | + client: c, |
| 62 | + reader: c, |
| 63 | + recorder: &internal.FakeRecorder{}, |
| 64 | + } |
| 65 | + |
| 66 | + req := reconcile.Request{ |
| 67 | + NamespacedName: types.NamespacedName{ |
| 68 | + Name: "test-build", |
| 69 | + Namespace: "default", |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + result, err := r.Reconcile(ctx, req) |
| 74 | + |
| 75 | + require.NoError(t, err) |
| 76 | + assert.Equal(t, reconcile.Result{RequeueAfter: requeueAfterDuration}, result) |
| 77 | + |
| 78 | + var updated v1.Build |
| 79 | + err = c.Get(ctx, req.NamespacedName, &updated) |
| 80 | + |
| 81 | + require.NoError(t, err) |
| 82 | + assert.NotNil(t, updated.Status, "status should not be nil") |
| 83 | + assert.NotEmpty(t, updated.Status.Phase, "phase should be set") |
| 84 | +} |
| 85 | + |
| 86 | +func TestReconcileBuildNotifyError(t *testing.T) { |
| 87 | + ctx := context.TODO() |
| 88 | + |
| 89 | + build := &v1.Build{ |
| 90 | + TypeMeta: metav1.TypeMeta{ |
| 91 | + Kind: string("Build"), |
| 92 | + }, |
| 93 | + ObjectMeta: metav1.ObjectMeta{ |
| 94 | + Name: "test-build", |
| 95 | + Namespace: "default", |
| 96 | + }, |
| 97 | + Spec: v1.BuildSpec{ |
| 98 | + Tasks: []v1.Task{ |
| 99 | + { |
| 100 | + Builder: &v1.BuilderTask{ |
| 101 | + BaseTask: v1.BaseTask{ |
| 102 | + Name: "builder", |
| 103 | + Configuration: v1.BuildConfiguration{ |
| 104 | + Strategy: v1.BuildStrategyPod, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + Status: v1.BuildStatus{ |
| 112 | + Phase: v1.BuildPhasePending, |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + c, err := internal.NewFakeClient(build) |
| 117 | + require.NoError(t, err) |
| 118 | + recorder := &internal.FakeRecorder{} |
| 119 | + r := &reconcileBuild{ |
| 120 | + client: c, |
| 121 | + reader: c, |
| 122 | + recorder: recorder, |
| 123 | + } |
| 124 | + |
| 125 | + req := reconcile.Request{ |
| 126 | + NamespacedName: types.NamespacedName{ |
| 127 | + Name: "test-build", |
| 128 | + Namespace: "default", |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + result, err := r.Reconcile(ctx, req) |
| 133 | + |
| 134 | + require.Error(t, err) |
| 135 | + assert.Equal(t, reconcile.Result{}, result) |
| 136 | + |
| 137 | + var updated v1.Build |
| 138 | + err = c.Get(ctx, req.NamespacedName, &updated) |
| 139 | + |
| 140 | + require.NoError(t, err) |
| 141 | + assert.NotNil(t, updated.Status, "status should not be nil") |
| 142 | + assert.NotEmpty(t, updated.Status.Phase, "phase should be set") |
| 143 | + assert.True(t, recorder.Called) |
| 144 | +} |
0 commit comments