From f389b8d36e4d61fa54c826c6a9c638a694eb93a5 Mon Sep 17 00:00:00 2001 From: Christian Busch Date: Thu, 3 Nov 2022 19:05:32 +0100 Subject: [PATCH 1/2] refactor: prepare helm tests - shorten DeckfileXXXSource types - add empty helm chart into resources - refactor fetch_source for enabeling helm tests later on --- getdeck/api/list.py | 2 +- getdeck/api/remove.py | 2 +- getdeck/deckfile/file.py | 50 +++++------ getdeck/fetch/fetch.py | 89 ++++++++++++------- getdeck/fetch/types.py | 20 ++--- getdeck/sources/generator.py | 20 ++--- getdeck/sources/selector.py | 30 +++---- test/resources/helm/.gitkeep | 0 test/resources/helm/empty/.helmignore | 23 +++++ test/resources/helm/empty/Chart.yaml | 24 +++++ .../helm/empty/templates/_helpers.tpl | 62 +++++++++++++ .../helm/empty/templates/deployment.yaml | 61 +++++++++++++ test/resources/helm/empty/templates/hpa.yaml | 28 ++++++ .../helm/empty/templates/ingress.yaml | 61 +++++++++++++ .../helm/empty/templates/service.yaml | 15 ++++ .../helm/empty/templates/serviceaccount.yaml | 12 +++ test/resources/helm/empty/values.yaml | 82 +++++++++++++++++ test/sources/deck.helm.yaml | 6 ++ test/src/fetch/test_fetch.py | 31 ++++++- test/src/fetch/test_source_fetcher.py | 24 ++--- test/src/sources/test_render_behavior.py | 4 +- 21 files changed, 535 insertions(+), 111 deletions(-) delete mode 100644 test/resources/helm/.gitkeep create mode 100644 test/resources/helm/empty/.helmignore create mode 100644 test/resources/helm/empty/Chart.yaml create mode 100644 test/resources/helm/empty/templates/_helpers.tpl create mode 100644 test/resources/helm/empty/templates/deployment.yaml create mode 100644 test/resources/helm/empty/templates/hpa.yaml create mode 100644 test/resources/helm/empty/templates/ingress.yaml create mode 100644 test/resources/helm/empty/templates/service.yaml create mode 100644 test/resources/helm/empty/templates/serviceaccount.yaml create mode 100644 test/resources/helm/empty/values.yaml diff --git a/getdeck/api/list.py b/getdeck/api/list.py index b038b7a..44dc84e 100644 --- a/getdeck/api/list.py +++ b/getdeck/api/list.py @@ -9,7 +9,7 @@ @stopwatch def get_available_decks(deckfile_location: str) -> List: - data_aux = fetch_data(deckfile_location, fetch_sources=False) + data_aux = fetch_data(deckfile_location, fetch_sources_flag=False) available_decks = data_aux.deckfile.get_decks() del data_aux diff --git a/getdeck/api/remove.py b/getdeck/api/remove.py index 394c375..5216072 100644 --- a/getdeck/api/remove.py +++ b/getdeck/api/remove.py @@ -17,7 +17,7 @@ def remove_cluster( ) -> bool: from getdeck.utils import ensure_cluster - data_aux = fetch_data(deckfile_location, fetch_sources=False) + data_aux = fetch_data(deckfile_location, fetch_sources_flag=False) k8s_provider = ensure_cluster( data_aux.deckfile, config, ignore_cluster, do_install=False ) diff --git a/getdeck/deckfile/file.py b/getdeck/deckfile/file.py index fb1abd0..3439cd7 100644 --- a/getdeck/deckfile/file.py +++ b/getdeck/deckfile/file.py @@ -34,7 +34,19 @@ def get_provider(self, config) -> AbstractProvider: raise e -class DeckfileHelmSource(BaseModel): +class InlineSource(BaseModel): + type: str = "inline" + content: Dict = None + + +class FileSource(BaseModel): + type: str = "file" + ref: str = None + targetRevision: Optional[str] = None + path: str = "" + + +class HelmSource(BaseModel): type: str = "helm" ref: str targetRevision: Optional[str] = None @@ -48,26 +60,14 @@ class DeckfileHelmSource(BaseModel): helmPlugins: List[str] = None -class DeckfileInlineSource(BaseModel): - type: str = "inline" - content: Dict = None - - -class DeckfileFileSource(BaseModel): - type: str = "file" - ref: str = None - targetRevision: Optional[str] = None - path: str = "" - - -class DeckfileKustomizeSource(BaseModel): +class KustomizeSource(BaseModel): type: str = "kustomize" ref: str targetRevision: Optional[str] = None path: str = "" -class DeckfileDirectorySource(BaseModel): +class DirectorySource(BaseModel): type: str = "directory" ref: str targetRevision: Optional[str] = None @@ -82,11 +82,11 @@ class DeckfileDeck(BaseModel): hosts: List[str] = [] sources: List[ Union[ - DeckfileInlineSource, - DeckfileFileSource, - DeckfileDirectorySource, - DeckfileHelmSource, - DeckfileKustomizeSource, + InlineSource, + FileSource, + DirectorySource, + HelmSource, + KustomizeSource, ] ] @@ -107,11 +107,11 @@ def __init__(self, *args, **data): source_type = "inline" source_class = { - "inline": DeckfileInlineSource, - "file": DeckfileFileSource, - "directory": DeckfileDirectorySource, - "kustomize": DeckfileKustomizeSource, - "helm": DeckfileHelmSource, + "inline": InlineSource, + "file": FileSource, + "directory": DirectorySource, + "kustomize": KustomizeSource, + "helm": HelmSource, }.get(source_type) self.sources.append(source_class(**source)) except KeyError: diff --git a/getdeck/fetch/fetch.py b/getdeck/fetch/fetch.py index aa05bc9..0586964 100644 --- a/getdeck/fetch/fetch.py +++ b/getdeck/fetch/fetch.py @@ -1,6 +1,6 @@ import logging import os -from typing import List +from typing import List, Union from getdeck.fetch.deck_fetcher import ( @@ -10,7 +10,14 @@ ) from getdeck.fetch.types import DataAux from getdeck.fetch.utils import detect_deckfile, get_path_and_name -from getdeck.deckfile.file import DeckfileDeck +from getdeck.deckfile.file import ( + DeckfileDeck, + DirectorySource, + FileSource, + HelmSource, + InlineSource, + KustomizeSource, +) from getdeck.deckfile.selector import deckfile_selector from getdeck.fetch.source_fetcher import ( @@ -27,7 +34,7 @@ class FetchError(Exception): pass -def _fetch_deck(data_aux: DataAux, location: str) -> DataAux: +def fetch_deck(data_aux: DataAux, location: str) -> DataAux: deckfile_aux = DeckfileAux(location=location) fetch_behavior = select_deck_fetch_behavior(location=location) if fetch_behavior: @@ -43,41 +50,55 @@ def _fetch_deck(data_aux: DataAux, location: str) -> DataAux: return data_aux -def _fetch_sources(deck: DeckfileDeck) -> List[SourceAux]: - source_fetcher = SourceFetcher(fetch_behavior=None) +def fetch_source( + source: Union[ + InlineSource, + FileSource, + DirectorySource, + HelmSource, + KustomizeSource, + ], + source_fetcher: SourceFetcher = None, +) -> SourceAux: + ref = getattr(source, "ref", None) + source_aux = SourceAux(location=ref) + source_aux.source = source # does not work during SourceAux initialization + + fetch_behavior = select_source_fetch_behavior(source=source) + if not fetch_behavior: + return source_aux + + if not source_fetcher: + source_fetcher = SourceFetcher(fetch_behavior=fetch_behavior) + else: + source_fetcher.fetch_behavior = fetch_behavior - source_auxs = [] - for source in deck.sources: - ref = getattr(source, "ref", None) - logger.info( - "Fetching source " f"{source.__class__.__name__}: {ref or 'no ref'}" - ) + try: + source_dict = source.dict() + source_aux = source_fetcher.fetch(data=source_aux, **source_dict) + except Exception as e: + logger.debug(str(e)) + del source_aux + raise FetchError(f"Source fetching error: {str(e)}") - source_aux = SourceAux(location=ref) - # assigning source during SourceAux initialization changes DeckfileHelmSource to DeckfileInlineSource.. ??? - source_aux.source = source + return source_aux # noqa: F821 - fetch_behavior = select_source_fetch_behavior(source=source) - source_fetcher.fetch_behavior = fetch_behavior - if not fetch_behavior: - source_auxs.append(source_aux) - continue - try: - source_dict = source.dict() - source_aux = source_fetcher.fetch(data=source_aux, **source_dict) - except Exception as e: - logger.debug(str(e)) - del source_aux, source_auxs[:] - raise FetchError(f"Source fetching error: {str(e)}") +def fetch_all_sources(deck: DeckfileDeck) -> List[SourceAux]: + source_fetcher = SourceFetcher(fetch_behavior=None) - source_auxs.append(source_aux) # noqa: F821 + source_auxs = [] + for source in deck.sources: + ref = getattr(source, "ref", None) + logger.info(f"Fetching {source.__class__.__name__}: {ref or '-'}") + source_aux = fetch_source(source=source, source_fetcher=source_fetcher) + source_auxs.append(source_aux) return source_auxs def fetch_data( - location: str, deck_name: str = None, fetch_sources: bool = True + location: str, deck_name: str = None, fetch_sources_flag: bool = True ) -> DataAux: """ delete returned DataAux to clean up temporary resources @@ -92,7 +113,7 @@ def fetch_data( # fetch deck data_aux = DataAux() - data_aux = _fetch_deck(data_aux=data_aux, location=location) + data_aux = fetch_deck(data_aux=data_aux, location=location) # validate file_detected = os.path.join(data_aux.deckfile_aux.path, data_aux.deckfile_aux.name) @@ -105,9 +126,11 @@ def fetch_data( data_aux.deckfile = deckfile # fetch sources - if fetch_sources: - deck = deckfile.get_deck(deck_name) - source_auxs = _fetch_sources(deck=deck) - data_aux.source_auxs = source_auxs + if not fetch_sources_flag: + return data_aux + + deck = deckfile.get_deck(deck_name) + source_auxs = fetch_all_sources(deck=deck) + data_aux.source_auxs = source_auxs return data_aux diff --git a/getdeck/fetch/types.py b/getdeck/fetch/types.py index 532004b..105fa78 100644 --- a/getdeck/fetch/types.py +++ b/getdeck/fetch/types.py @@ -8,11 +8,11 @@ from getdeck import configuration from getdeck.deckfile.file import ( - DeckfileDirectorySource, - DeckfileFileSource, - DeckfileHelmSource, - DeckfileInlineSource, - DeckfileKustomizeSource, + DirectorySource, + FileSource, + HelmSource, + InlineSource, + KustomizeSource, ) @@ -30,11 +30,11 @@ class SourceAux(BaseModel): source: Optional[ Union[ - DeckfileInlineSource, - DeckfileFileSource, - DeckfileDirectorySource, - DeckfileHelmSource, - DeckfileKustomizeSource, + InlineSource, + FileSource, + DirectorySource, + HelmSource, + KustomizeSource, ] ] = None diff --git a/getdeck/sources/generator.py b/getdeck/sources/generator.py index 0326230..f2e43d0 100644 --- a/getdeck/sources/generator.py +++ b/getdeck/sources/generator.py @@ -4,11 +4,11 @@ from getdeck.configuration import ClientConfiguration from getdeck.deckfile.file import ( - DeckfileDirectorySource, - DeckfileFileSource, - DeckfileInlineSource, - DeckfileKustomizeSource, - DeckfileHelmSource, + DirectorySource, + FileSource, + InlineSource, + KustomizeSource, + HelmSource, ) from getdeck.fetch.types import DeckfileAux, SourceAux from getdeck.sources.types import K8sSourceFile @@ -25,11 +25,11 @@ def __init__( self, path: str, source: Union[ - DeckfileInlineSource, - DeckfileFileSource, - DeckfileDirectorySource, - DeckfileHelmSource, - DeckfileKustomizeSource, + InlineSource, + FileSource, + DirectorySource, + HelmSource, + KustomizeSource, ], config: ClientConfiguration, namespace: str, diff --git a/getdeck/sources/selector.py b/getdeck/sources/selector.py index a8a7a4f..34fb05f 100644 --- a/getdeck/sources/selector.py +++ b/getdeck/sources/selector.py @@ -1,10 +1,10 @@ from typing import Union, Optional from getdeck.deckfile.file import ( - DeckfileDirectorySource, - DeckfileFileSource, - DeckfileHelmSource, - DeckfileInlineSource, - DeckfileKustomizeSource, + DirectorySource, + FileSource, + HelmSource, + InlineSource, + KustomizeSource, ) from getdeck.sources.generator import RenderBehavior from getdeck.sources.file import File @@ -15,18 +15,18 @@ def select_render_behavior( source: Union[ - DeckfileInlineSource, - DeckfileFileSource, - DeckfileDirectorySource, - DeckfileHelmSource, - DeckfileKustomizeSource, + InlineSource, + FileSource, + DirectorySource, + HelmSource, + KustomizeSource, ], ) -> Optional[RenderBehavior]: render_behavior = { - DeckfileInlineSource: Inline, - DeckfileFileSource: File, - DeckfileDirectorySource: File, - DeckfileHelmSource: Helm, - DeckfileKustomizeSource: Kustomize, + InlineSource: Inline, + FileSource: File, + DirectorySource: File, + HelmSource: Helm, + KustomizeSource: Kustomize, }.get(type(source), None) return render_behavior diff --git a/test/resources/helm/.gitkeep b/test/resources/helm/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/test/resources/helm/empty/.helmignore b/test/resources/helm/empty/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/test/resources/helm/empty/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/test/resources/helm/empty/Chart.yaml b/test/resources/helm/empty/Chart.yaml new file mode 100644 index 0000000..a5200c3 --- /dev/null +++ b/test/resources/helm/empty/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: empty +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "1.16.0" diff --git a/test/resources/helm/empty/templates/_helpers.tpl b/test/resources/helm/empty/templates/_helpers.tpl new file mode 100644 index 0000000..f3e1f35 --- /dev/null +++ b/test/resources/helm/empty/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "empty.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "empty.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "empty.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "empty.labels" -}} +helm.sh/chart: {{ include "empty.chart" . }} +{{ include "empty.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "empty.selectorLabels" -}} +app.kubernetes.io/name: {{ include "empty.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "empty.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "empty.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/test/resources/helm/empty/templates/deployment.yaml b/test/resources/helm/empty/templates/deployment.yaml new file mode 100644 index 0000000..d7e0e3e --- /dev/null +++ b/test/resources/helm/empty/templates/deployment.yaml @@ -0,0 +1,61 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "empty.fullname" . }} + labels: + {{- include "empty.labels" . | nindent 4 }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "empty.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "empty.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "empty.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 80 + protocol: TCP + livenessProbe: + httpGet: + path: / + port: http + readinessProbe: + httpGet: + path: / + port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/test/resources/helm/empty/templates/hpa.yaml b/test/resources/helm/empty/templates/hpa.yaml new file mode 100644 index 0000000..0644a84 --- /dev/null +++ b/test/resources/helm/empty/templates/hpa.yaml @@ -0,0 +1,28 @@ +{{- if .Values.autoscaling.enabled }} +apiVersion: autoscaling/v2beta1 +kind: HorizontalPodAutoscaler +metadata: + name: {{ include "empty.fullname" . }} + labels: + {{- include "empty.labels" . | nindent 4 }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ include "empty.fullname" . }} + minReplicas: {{ .Values.autoscaling.minReplicas }} + maxReplicas: {{ .Values.autoscaling.maxReplicas }} + metrics: + {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} + - type: Resource + resource: + name: cpu + targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} + {{- end }} + {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} + - type: Resource + resource: + name: memory + targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} + {{- end }} +{{- end }} diff --git a/test/resources/helm/empty/templates/ingress.yaml b/test/resources/helm/empty/templates/ingress.yaml new file mode 100644 index 0000000..3b5fa78 --- /dev/null +++ b/test/resources/helm/empty/templates/ingress.yaml @@ -0,0 +1,61 @@ +{{- if .Values.ingress.enabled -}} +{{- $fullName := include "empty.fullname" . -}} +{{- $svcPort := .Values.service.port -}} +{{- if and .Values.ingress.className (not (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion)) }} + {{- if not (hasKey .Values.ingress.annotations "kubernetes.io/ingress.class") }} + {{- $_ := set .Values.ingress.annotations "kubernetes.io/ingress.class" .Values.ingress.className}} + {{- end }} +{{- end }} +{{- if semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1 +{{- else if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}} +apiVersion: networking.k8s.io/v1beta1 +{{- else -}} +apiVersion: extensions/v1beta1 +{{- end }} +kind: Ingress +metadata: + name: {{ $fullName }} + labels: + {{- include "empty.labels" . | nindent 4 }} + {{- with .Values.ingress.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + {{- if and .Values.ingress.className (semverCompare ">=1.18-0" .Capabilities.KubeVersion.GitVersion) }} + ingressClassName: {{ .Values.ingress.className }} + {{- end }} + {{- if .Values.ingress.tls }} + tls: + {{- range .Values.ingress.tls }} + - hosts: + {{- range .hosts }} + - {{ . | quote }} + {{- end }} + secretName: {{ .secretName }} + {{- end }} + {{- end }} + rules: + {{- range .Values.ingress.hosts }} + - host: {{ .host | quote }} + http: + paths: + {{- range .paths }} + - path: {{ .path }} + {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} + pathType: {{ .pathType }} + {{- end }} + backend: + {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} + service: + name: {{ $fullName }} + port: + number: {{ $svcPort }} + {{- else }} + serviceName: {{ $fullName }} + servicePort: {{ $svcPort }} + {{- end }} + {{- end }} + {{- end }} +{{- end }} diff --git a/test/resources/helm/empty/templates/service.yaml b/test/resources/helm/empty/templates/service.yaml new file mode 100644 index 0000000..a795286 --- /dev/null +++ b/test/resources/helm/empty/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "empty.fullname" . }} + labels: + {{- include "empty.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "empty.selectorLabels" . | nindent 4 }} diff --git a/test/resources/helm/empty/templates/serviceaccount.yaml b/test/resources/helm/empty/templates/serviceaccount.yaml new file mode 100644 index 0000000..a7afb2a --- /dev/null +++ b/test/resources/helm/empty/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "empty.serviceAccountName" . }} + labels: + {{- include "empty.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/test/resources/helm/empty/values.yaml b/test/resources/helm/empty/values.yaml new file mode 100644 index 0000000..3df9e4b --- /dev/null +++ b/test/resources/helm/empty/values.yaml @@ -0,0 +1,82 @@ +# Default values for empty. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: nginx + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + +podAnnotations: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +ingress: + enabled: false + className: "" + annotations: {} + # kubernetes.io/ingress.class: nginx + # kubernetes.io/tls-acme: "true" + hosts: + - host: chart-example.local + paths: + - path: / + pathType: ImplementationSpecific + tls: [] + # - secretName: chart-example-tls + # hosts: + # - chart-example.local + +resources: {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +nodeSelector: {} + +tolerations: [] + +affinity: {} diff --git a/test/sources/deck.helm.yaml b/test/sources/deck.helm.yaml index 85bf974..347a3d5 100644 --- a/test/sources/deck.helm.yaml +++ b/test/sources/deck.helm.yaml @@ -43,3 +43,9 @@ decks: value: false - name: controller.ingressClassResource.default value: true + + - type: helm + ref: ../resources/helm/empty + path: . + chart: empty + releaseName: empty diff --git a/test/src/fetch/test_fetch.py b/test/src/fetch/test_fetch.py index a8a7df4..6ebb573 100644 --- a/test/src/fetch/test_fetch.py +++ b/test/src/fetch/test_fetch.py @@ -1,6 +1,33 @@ from unittest import TestCase +from getdeck.deckfile.file import FileSource, InlineSource -from getdeck.fetch.fetch import fetch_data +from getdeck.fetch.fetch import fetch_data, fetch_source + + +class FetchSourceTest(TestCase): + def test_local_inline(self): + source = InlineSource(content={}) + source_aux = fetch_source(source=source) + self.assertIsNotNone(source_aux.source) + + def test_local_file(self): + source = FileSource(ref="./test/resources/file/hello.yaml") + source_aux = fetch_source(source=source) + self.assertIsNotNone(source_aux.source) + self.assertEqual(source_aux.location, "./test/resources/file/hello.yaml") + self.assertEqual(source_aux.name, "hello.yaml") + self.assertEqual(source_aux.path, "./test/resources/file") + + def test_git_file(self): + source = FileSource( + ref="git@github.com:Getdeck/getdeck.git", + path="test/resources/test/hello.yaml", + ) + source_aux = fetch_source(source=source) + self.assertIsNotNone(source_aux.source) + self.assertEqual(source_aux.location, "git@github.com:Getdeck/getdeck.git") + self.assertEqual(source_aux.name, "hello.yaml") + self.assertIn("/test/resources/test", source_aux.path) class FetchDataTest(TestCase): @@ -30,7 +57,7 @@ def test_local_helm(self): data_aux = fetch_data(location) self.assertIsNotNone(data_aux.deckfile) self.assertIsNotNone(data_aux.deckfile_aux) - self.assertEqual(len(data_aux.source_auxs), 1) + self.assertEqual(len(data_aux.source_auxs), 2) def test_git_with_no_deckfile(self): location = "git@github.com:Getdeck/getdeck.git" diff --git a/test/src/fetch/test_source_fetcher.py b/test/src/fetch/test_source_fetcher.py index 9e75383..358f05b 100644 --- a/test/src/fetch/test_source_fetcher.py +++ b/test/src/fetch/test_source_fetcher.py @@ -1,8 +1,8 @@ from unittest import TestCase from getdeck.deckfile.file import ( - DeckfileFileSource, - DeckfileHelmSource, - DeckfileInlineSource, + FileSource, + HelmSource, + InlineSource, ) from getdeck.fetch.source_fetcher import ( Git, @@ -16,46 +16,46 @@ class SelectSourceFetchBehaviorTest(TestCase): def test_git_file_source(self): - source = DeckfileFileSource(ref="git@github.com:Getdeck/getdeck.git") + source = FileSource(ref="git@github.com:Getdeck/getdeck.git") fetch_behavior = select_source_fetch_behavior(source=source) self.assertIsInstance(fetch_behavior, Git) def test_git_file_source_https(self): - source = DeckfileFileSource(ref="https://github.com/Getdeck/getdeck.git") + source = FileSource(ref="https://github.com/Getdeck/getdeck.git") fetch_behavior = select_source_fetch_behavior(source=source) self.assertIsInstance(fetch_behavior, Git) def test_git_helm_source(self): - source = DeckfileHelmSource( + source = HelmSource( ref="git@github.com:Getdeck/getdeck.git", releaseName="test" ) fetch_behavior = select_source_fetch_behavior(source=source) self.assertIsInstance(fetch_behavior, Git) def test_http_file_source(self): - source = DeckfileFileSource( + source = FileSource( ref="https://raw.githubusercontent.com/Getdeck/getdeck/main/test/sources/resources/hello.yaml" ) fetch_behavior = select_source_fetch_behavior(source=source) self.assertIsInstance(fetch_behavior, Http) def test_local_file_source_dot(self): - source = DeckfileFileSource(ref=".") + source = FileSource(ref=".") fetch_behavior = select_source_fetch_behavior(source=source) self.assertIsInstance(fetch_behavior, Local) def test_local_file_source_path(self): - source = DeckfileFileSource(ref="./test/hello.yaml") + source = FileSource(ref="./test/hello.yaml") fetch_behavior = select_source_fetch_behavior(source=source) self.assertIsInstance(fetch_behavior, Local) def test_none_inline_source(self): - source = DeckfileInlineSource(content={}) + source = InlineSource(content={}) fetch_behavior = select_source_fetch_behavior(source=source) self.assertIsNone(fetch_behavior) def test_none_helm_source(self): - source = DeckfileHelmSource( + source = HelmSource( ref="https://kubernetes.github.io/dashboard/", chart="kubernetes-dashboard", releaseName="dashboard", @@ -67,7 +67,7 @@ def test_none_helm_source(self): class SourceFetcherTest(TestCase): def test_file(self): location = "https://raw.githubusercontent.com/Getdeck/getdeck/main/test/sources/resources/hello.yaml" - source = DeckfileFileSource(ref=location) + source = FileSource(ref=location) source_aux = SourceAux(location=location) source_aux.source = source diff --git a/test/src/sources/test_render_behavior.py b/test/src/sources/test_render_behavior.py index 0e15dca..e85e779 100644 --- a/test/src/sources/test_render_behavior.py +++ b/test/src/sources/test_render_behavior.py @@ -1,5 +1,5 @@ from unittest import TestCase -from getdeck.deckfile.file import DeckfileInlineSource +from getdeck.deckfile.file import InlineSource from getdeck.fetch.types import DeckfileAux, SourceAux from getdeck.sources.inline import Inline import itertools @@ -16,7 +16,7 @@ def test_render(self): content = combination[1] deckfile_aux = DeckfileAux(location="test") - source_aux = SourceAux(source=DeckfileInlineSource(content=content)) + source_aux = SourceAux(source=InlineSource(content=content)) render_behavior = Inline( path=None, From 769324cf4d724214a2d9ace687de7ab8480738c2 Mon Sep 17 00:00:00 2001 From: Christian Busch Date: Thu, 3 Nov 2022 19:13:08 +0100 Subject: [PATCH 2/2] fix: code smell --- test/src/fetch/test_fetch.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/src/fetch/test_fetch.py b/test/src/fetch/test_fetch.py index 6ebb573..f8d1560 100644 --- a/test/src/fetch/test_fetch.py +++ b/test/src/fetch/test_fetch.py @@ -4,6 +4,9 @@ from getdeck.fetch.fetch import fetch_data, fetch_source +GIT_REF = "git@github.com:Getdeck/getdeck.git" + + class FetchSourceTest(TestCase): def test_local_inline(self): source = InlineSource(content={}) @@ -20,12 +23,12 @@ def test_local_file(self): def test_git_file(self): source = FileSource( - ref="git@github.com:Getdeck/getdeck.git", + ref=GIT_REF, path="test/resources/test/hello.yaml", ) source_aux = fetch_source(source=source) self.assertIsNotNone(source_aux.source) - self.assertEqual(source_aux.location, "git@github.com:Getdeck/getdeck.git") + self.assertEqual(source_aux.location, GIT_REF) self.assertEqual(source_aux.name, "hello.yaml") self.assertIn("/test/resources/test", source_aux.path) @@ -60,7 +63,7 @@ def test_local_helm(self): self.assertEqual(len(data_aux.source_auxs), 2) def test_git_with_no_deckfile(self): - location = "git@github.com:Getdeck/getdeck.git" + location = GIT_REF with self.assertRaises(RuntimeError): _ = fetch_data(location)