internal/object: setter/getter suspend & artifact
Add setters and getters for spec.suspend and status.artifact. This is needed for writing generic tests for any source kind. Signed-off-by: Sunny <darkowlzz@protonmail.com>
This commit is contained in:
parent
663b6a78a2
commit
15b4f96930
|
|
@ -17,11 +17,14 @@ limitations under the License.
|
|||
package object
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
|
||||
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -112,3 +115,59 @@ func GetRequeueInterval(obj runtime.Object) (time.Duration, error) {
|
|||
}
|
||||
return time.ParseDuration(interval)
|
||||
}
|
||||
|
||||
// GetSuspend returns the spec.suspend of a given runtime object.
|
||||
func GetSuspend(obj runtime.Object) (bool, error) {
|
||||
u, err := toUnstructured(obj)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
suspend, found, err := unstructured.NestedBool(u.Object, "spec", "suspend")
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
// Since suspend is an optional field, it's false when not found.
|
||||
if !found {
|
||||
return false, nil
|
||||
}
|
||||
return suspend, nil
|
||||
}
|
||||
|
||||
// SetSuspend sets the spec.suspend value of a given runtime object.
|
||||
func SetSuspend(obj runtime.Object, val bool) error {
|
||||
content, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
u := unstructured.Unstructured{}
|
||||
u.SetUnstructuredContent(content)
|
||||
if err := unstructured.SetNestedField(u.Object, val, "spec", "suspend"); err != nil {
|
||||
return err
|
||||
}
|
||||
return runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, obj)
|
||||
}
|
||||
|
||||
// GetArtifact returns the status.artifact of a given runtime object.
|
||||
func GetArtifact(obj runtime.Object) (*sourcev1.Artifact, error) {
|
||||
u, err := toUnstructured(obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
artifact, found, err := unstructured.NestedFieldNoCopy(u.Object, "status", "artifact")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Since artifact is an optional field, return nil when not found.
|
||||
if !found {
|
||||
return nil, nil
|
||||
}
|
||||
enc, err := json.Marshal(artifact)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
outArtifact := &sourcev1.Artifact{}
|
||||
if err := json.Unmarshal(enc, outArtifact); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return outArtifact, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -86,3 +86,51 @@ func TestGetRequeueInterval(t *testing.T) {
|
|||
_, err = GetRequeueInterval(obj2)
|
||||
g.Expect(err).To(Equal(ErrRequeueIntervalNotFound))
|
||||
}
|
||||
|
||||
func TestGetSuspend(t *testing.T) {
|
||||
g := NewWithT(t)
|
||||
|
||||
// Get unset suspend value.
|
||||
obj := &sourcev1.GitRepository{}
|
||||
suspend, err := GetSuspend(obj)
|
||||
g.Expect(err).ToNot(HaveOccurred())
|
||||
g.Expect(suspend).To(BeFalse())
|
||||
|
||||
// Get set suspend value.
|
||||
obj.Spec.Suspend = true
|
||||
suspend, err = GetSuspend(obj)
|
||||
g.Expect(err).ToNot(HaveOccurred())
|
||||
g.Expect(suspend).To(BeTrue())
|
||||
}
|
||||
|
||||
func TestSetSuspend(t *testing.T) {
|
||||
g := NewWithT(t)
|
||||
|
||||
obj := &sourcev1.GitRepository{}
|
||||
err := SetSuspend(obj, true)
|
||||
g.Expect(err).ToNot(HaveOccurred())
|
||||
g.Expect(obj.Spec.Suspend).To(BeTrue())
|
||||
|
||||
// Overwrite previous value.
|
||||
err = SetSuspend(obj, false)
|
||||
g.Expect(err).ToNot(HaveOccurred())
|
||||
g.Expect(obj.Spec.Suspend).To(BeFalse())
|
||||
}
|
||||
|
||||
func TestGetArtifact(t *testing.T) {
|
||||
g := NewWithT(t)
|
||||
|
||||
// Get unset artifact value.
|
||||
obj := &sourcev1.GitRepository{}
|
||||
artifact, err := GetArtifact(obj)
|
||||
g.Expect(err).ToNot(HaveOccurred())
|
||||
g.Expect(artifact).To(BeNil())
|
||||
|
||||
// Get set artifact value.
|
||||
obj.Status.Artifact = &sourcev1.Artifact{Path: "aaa", Revision: "zzz"}
|
||||
artifact, err = GetArtifact(obj)
|
||||
g.Expect(err).ToNot(HaveOccurred())
|
||||
g.Expect(artifact).ToNot(BeNil())
|
||||
g.Expect(artifact.Path).To(Equal("aaa"))
|
||||
g.Expect(artifact.Revision).To(Equal("zzz"))
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue