Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SkYNewZ committed Sep 12, 2022
1 parent f56c598 commit 03c3d4a
Show file tree
Hide file tree
Showing 24 changed files with 771 additions and 103 deletions.
26 changes: 17 additions & 9 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ permissions:

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
Expand All @@ -25,20 +24,20 @@ jobs:
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
images: ${{ env.REGISTRY }}/${{ github.repository }}

- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
uses: docker/build-push-action@v3
with:
context: .
push: true
Expand All @@ -59,8 +58,17 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3

- name: Deploy manifests
run: make install
- name: Lowercase repository # Docker images cannot contains uppercase
id: image
uses: ASzc/change-string-case-action@v2
with:
string: ${{ github.repository }}

- name: Set kubernetes context
uses: azure/k8s-set-context@v3
with:
method: kubeconfig
kubeconfig: ${{ secrets.KUBECONFIG }}

- name: Deploy operator
run: echo "deploying ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.tags }}..."
- name: Install CRDs and deploy operator
run: make deploy IMG="${{ env.REGISTRY }}/${{ steps.image.outputs.lowercase }}:${{ github.ref_name }}"
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
run:
timeout: 5m
tests: true
tests: false
modules-download-mode: readonly

linters:
Expand Down Expand Up @@ -33,3 +33,4 @@ linters:
- deadcode
- varcheck
- ifshort
- gci
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ vet: ## Run go vet against code.
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out

.PHONY: lint
lint: golangci-lint ## Run golangci-lint against code.
$(GOLANGCI_LINT) run --fix

##@ Build

.PHONY: build
Expand Down Expand Up @@ -105,6 +109,7 @@ uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified
.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
cd config/default && $(KUSTOMIZE) edit set label "app.kubernetes.io/version:${VERSION}"
$(KUSTOMIZE) build config/default | kubectl apply -f -

.PHONY: undeploy
Expand All @@ -123,6 +128,7 @@ KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
ENVTEST ?= $(LOCALBIN)/setup-envtest
IFACEMAKER ?= $(LOCALBIN)/ifacemaker
GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint

## Tool Versions
KUSTOMIZE_VERSION ?= v4.5.5
Expand All @@ -147,4 +153,8 @@ $(ENVTEST): $(LOCALBIN)
.PHONY: ifacemaker
ifacemaker: $(IFACEMAKER) ## Download ifacemaker locally if necessary.
$(IFACEMAKER): $(LOCALBIN)
test -s $(LOCALBIN)/ifacemaker || GOBIN=$(LOCALBIN) go install github.com/vburenin/ifacemaker@latest
test -s $(LOCALBIN)/ifacemaker || GOBIN=$(LOCALBIN) go install github.com/vburenin/ifacemaker@latest

golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
$(GOLANGCI_LINT): $(LOCALBIN)
test -s $(LOCALBIN)/golangci-lint || GOBIN=$(LOCALBIN) go install github.com/golangci/golangci-lint/cmd/[email protected]
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,5 @@ limitations under the License.

## TODOs

- [ ] Write unit tests
- [ ] Write integration tests
- [ ] Run integrations tests in CI
- [ ] Deploy operator from CI
- [ ] Setup events
- [ ] Run integrations tests in CI
2 changes: 1 addition & 1 deletion api/v1alpha1/feed_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// AuthSecretReference references a Secret containing a Put.io authentication token
// AuthSecretReference references a Secret containing a Put.io authentication token.
type AuthSecretReference struct {
// +kubebuilder:validation:MinLength=1
Name string `json:"name"`
Expand Down
62 changes: 62 additions & 0 deletions api/v1alpha1/feed_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package v1alpha1

import (
"reflect"
"testing"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestFeed_AuthSecretRef(t *testing.T) {
type fields struct {
TypeMeta v1.TypeMeta
ObjectMeta v1.ObjectMeta
Spec FeedSpec
Status FeedStatus
}
tests := []struct {
name string
fields fields
want AuthSecretReference
}{
{
name: "expected",
fields: fields{
TypeMeta: v1.TypeMeta{},
ObjectMeta: v1.ObjectMeta{},
Spec: FeedSpec{
Title: "",
RssSourceURL: "",
ParentDirID: nil,
DeleteOldFiles: false,
DontProcessWholeFeed: false,
Keyword: "",
UnwantedKeywords: "",
Paused: false,
AuthSecretRef: AuthSecretReference{
Name: "foo",
Key: "bar",
},
},
Status: FeedStatus{},
},
want: AuthSecretReference{
Name: "foo",
Key: "bar",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
in := &Feed{
TypeMeta: tt.fields.TypeMeta,
ObjectMeta: tt.fields.ObjectMeta,
Spec: tt.fields.Spec,
Status: tt.fields.Status,
}
if got := in.AuthSecretRef(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AuthSecretRef() = %v, want %v", got, tt.want)
}
})
}
}
11 changes: 6 additions & 5 deletions config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ namespace: putio-operator-system
namePrefix: putio-operator-

# Labels to add to all resources and selectors.
#labels:
#- includeSelectors: true
# pairs:
# someName: someValue
labels:
- includeSelectors: true
pairs:
app.kubernetes.io/part-of: putio-operator
app.kubernetes.io/managed-by: kubectl

resources:
- ../crd
Expand All @@ -34,7 +35,7 @@ patchesStrategicMerge:

# Mount the controller config file for loading manager configurations
# through a ComponentConfig type
#- manager_config_patch.yaml
- manager_config_patch.yaml

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
Expand Down
5 changes: 0 additions & 5 deletions config/default/manager_auth_proxy_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,3 @@ spec:
requests:
cpu: 5m
memory: 64Mi
- name: manager
args:
- "--health-probe-bind-address=:8081"
- "--metrics-bind-address=127.0.0.1:8080"
- "--leader-elect"
2 changes: 0 additions & 2 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ spec:
containers:
- command:
- /manager
args:
- --leader-elect
image: controller:latest
name: manager
securityContext:
Expand Down
10 changes: 9 additions & 1 deletion config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ metadata:
name: manager-role
rules:
- apiGroups:
- skynewz.dev
- ""
resources:
- events
verbs:
- create
- patch
- apiGroups:
- ""
resources:
- secrets
verbs:
- get
- list
- watch
- apiGroups:
- skynewz.dev
resources:
Expand Down
52 changes: 26 additions & 26 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@ const finalizerAnnotation string = "feed.skynewz.dev/finalizer"

const (
eventReconciliationStarted string = "ReconciliationStarted"
eventUnableToGetAuthSecret = "UnableToGetAuthSecret"

// finalizer events
eventAddedFinalizer = "InstanceFinalizerAdded"
eventUnableToAddFinalizer = "UnableToAddFinalizer"
eventUnableToDeleteFinalizer = "UnableToDeleteFinalizer"

// creation/update events
eventCreateOrUpdatedAtPutio = "CreateOrUpdatedAtPutio"
eventUnableToCreateOrUpdatedAtPutio = "UnableToCreateOrUpdatedAtPutio"
eventSuccessfullyCreateOrUpdatedAtPutio = "SuccessfullyCreateOrUpdatedAtPutio"

// deletion event
eventDeleteFeedAtPutio = "DeleteFeedAtPutio"
eventUnableToDeleteAtPutio = "UnableToDeleteAtPutio"
eventSuccessfullyDeletedAtPutio = "SuccessfullyDeletedAtPutio"

// pause status update
eventSetPauseStatus = "SetPauseStatus"
eventUnableToSetPauseStatus = "UnableToSetPauseStatus"
eventSuccessfullySetPauseStatus = "SuccessfullySetPauseStatus"

// status update
eventFeedStatus = "FeedStatus"
eventUnableToUpdateFeedStatus = "UnableToUpdateFeedStatus"
eventFeedStatusSuccessfullyUpdated = "FeedStatusSuccessfullyUpdated"
eventUnableToGetAuthSecret string = "UnableToGetAuthSecret" //nolint:gosec

// finalizer events.
eventAddedFinalizer string = "InstanceFinalizerAdded"
eventUnableToAddFinalizer string = "UnableToAddFinalizer"
eventUnableToDeleteFinalizer string = "UnableToDeleteFinalizer"

// creation/update events.
eventCreateOrUpdatedAtPutio string = "CreateOrUpdatedAtPutio"
eventUnableToCreateOrUpdatedAtPutio string = "UnableToCreateOrUpdatedAtPutio"
eventSuccessfullyCreateOrUpdatedAtPutio string = "SuccessfullyCreateOrUpdatedAtPutio"

// deletion event.
eventDeleteFeedAtPutio string = "DeleteFeedAtPutio"
eventUnableToDeleteAtPutio string = "UnableToDeleteAtPutio"
eventSuccessfullyDeletedAtPutio string = "SuccessfullyDeletedAtPutio"

// pause status update.
eventSetPauseStatus string = "SetPauseStatus"
eventUnableToSetPauseStatus string = "UnableToSetPauseStatus"
eventSuccessfullySetPauseStatus string = "SuccessfullySetPauseStatus"

// status update.
eventFeedStatus string = "FeedStatus"
eventUnableToUpdateFeedStatus string = "UnableToUpdateFeedStatus"
eventFeedStatusSuccessfullyUpdated string = "FeedStatusSuccessfullyUpdated"
)

type FeedConditionType string
Expand Down
Loading

0 comments on commit 03c3d4a

Please sign in to comment.