Kubernetes の偽クライアントを使用した単体テスト
Kubernetes と対話するコードのテストを作成する場合、テスト環境を実際のクラスターから分離すると有益です。これは、ライブ クラスターを必要とせずに Kubernetes API の動作をシミュレートする偽のクライアントを利用することで実現できます。
問題
次の方法を検討してください。
<code class="go">import ( "fmt" "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" fake "k8s.io/client-go/kubernetes/fake" "time" ) func GetNamespaceCreationTime(namespace string) int64 { clientset, err := kubernetes.NewForConfig(rest.InClusterConfig()) if err != nil { panic(err.Error()) } ns, err := clientset.CoreV1().Namespaces().Get(namespace, metav1.GetOptions{}) if err != nil { panic(err.Error()) } fmt.Printf("%v \n", ns.CreationTimestamp) return (ns.GetCreationTimestamp().Unix()) }</code>
目標は、偽のクライアントを使用してこのメソッドの単体テストを作成することです。
解決策
偽のクライアントを使用するには、パラメーターとして kubernetes.Interface を受け入れるように GetNamespaceCreationTime 関数を変更する必要があります。 :
<code class="go">func GetNamespaceCreationTime(kubeClient kubernetes.Interface, namespace string) int64 { ns, err := kubeClient.CoreV1().Namespaces().Get(namespace, metav1.GetOptions{}) if err != nil { panic(err.Error()) } fmt.Printf("%v \n", ns.CreationTimestamp) return (ns.GetCreationTimestamp().Unix()) }</code>
テスト関数では、次のように偽のクライアントセットを作成し、それを GetNamespaceCreationTime メソッドに渡すことができます:
<code class="go">func TestGetNamespaceCreationTime(t *testing.T) { kubeClient := fake.NewSimpleClientset() got := GetNamespaceCreationTime(kubeClient, "default") want := int64(1257894000) nsMock :=kubeClient.CoreV1().Namespaces() nsMock.Create(&v1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: "default", CreationTimestamp: metav1.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), }, }) if got != want { t.Errorf("got %q want %q", got, want) } }</code>
クラスター内構成スタブによるテストの完了
クラスター内構成のスタブを使用した完全なテストは次のようになります:
<code class="go">import ( "fmt" "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" fake "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "time" ) var getInclusterConfigFunc = rest.InClusterConfig var getNewKubeClientFunc = dynamic.NewForConfig func GetNamespaceCreationTime(kubeClient kubernetes.Interface, namespace string) int64 { ns, err := kubeClient.CoreV1().Namespaces().Get(namespace, metav1.GetOptions{}) if err != nil { panic(err.Error()) } fmt.Printf("%v \n", ns.CreationTimestamp) return (ns.GetCreationTimestamp().Unix()) } func GetClientSet() kubernetes.Interface { config, err := getInclusterConfigFunc() if err != nil { log.Warnf("Could not get in-cluster config: %s", err) return nil, err } client, err := getNewKubeClientFunc(config) if err != nil { log.Warnf("Could not connect to in-cluster API server: %s", err) return nil, err } return client, err } func TestGetNamespaceCreationTime(t *testing.T) { kubeClient := fake.NewSimpleClientset() got := GetNamespaceCreationTime(kubeClient, "default") want := int64(1257894000) nsMock :=kubeClient.CoreV1().Namespaces() nsMock.Create(&v1.Namespace{ ObjectMeta: metav1.ObjectMeta{ Name: "default", CreationTimestamp: metav1.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), }, }) if got != want { t.Errorf("got %q want %q", got, want) } } func fakeGetInclusterConfig() (*rest.Config, error) { return nil, nil } func fakeGetInclusterConfigWithError() (*rest.Config, error) { return nil, errors.New("fake error getting in-cluster config") } func TestGetInclusterKubeClient(t *testing.T) { origGetInclusterConfig := getInclusterConfigFunc getInclusterConfigFunc = fakeGetInclusterConfig origGetNewKubeClient := getNewKubeClientFunc getNewKubeClientFunc = fakeGetNewKubeClient defer func() { getInclusterConfigFunc = origGetInclusterConfig getNewKubeClientFunc = origGetNewKubeClient }() client, err := GetClientSet() assert.Nil(t, client, "Client is not nil") assert.Nil(t, err, "error is not nil") } func TestGetInclusterKubeClient_ConfigError(t *testing.T) { origGetInclusterConfig := getInclusterConfigFunc getInclusterConfigFunc = fakeGetInclusterConfigWithError defer func() { getInclusterConfigFunc = origGetInclusterConfig }() client, err := GetClientSet() assert.Nil(t, client, "Client is not nil") assert.NotNil(t, err, "error is nil") }</code>
以上がKubernetes 統合コードの単体テストに偽のクライアントを使用する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

GolangとPythonの主な違いは、並行性モデル、タイプシステム、パフォーマンス、実行速度です。 1. GolangはCSPモデルを使用します。これは、同時タスクの高いタスクに適しています。 Pythonは、I/O集約型タスクに適したマルチスレッドとGILに依存しています。 2。Golangは静的なタイプで、Pythonは動的なタイプです。 3.ゴーランコンパイルされた言語実行速度は高速であり、Python解釈言語開発は高速です。

Golangは通常Cよりも遅くなりますが、Golangはプログラミングと開発効率の同時により多くの利点があります。1)Golangのゴミ収集と並行性モデルにより、同時性の高いシナリオではうまく機能します。 2)Cは、手動のメモリ管理とハードウェアの最適化により、より高いパフォーマンスを取得しますが、開発の複雑さが高くなります。

GolangはクラウドコンピューティングとDevOpsで広く使用されており、その利点はシンプルさ、効率性、および同時プログラミング機能にあります。 1)クラウドコンピューティングでは、GolangはGoroutineおよびチャネルメカニズムを介して同時リクエストを効率的に処理します。 2)DevOpsでは、Golangの高速コンピレーションとクロスプラットフォーム機能により、自動化ツールの最初の選択肢になります。

GolangとCにはそれぞれ、パフォーマンス効率に独自の利点があります。 1)GolangはGoroutineおよびGarbage Collectionを通じて効率を向上させますが、一時停止時間を導入する場合があります。 2)Cは、手動のメモリ管理と最適化を通じて高性能を実現しますが、開発者はメモリリークやその他の問題に対処する必要があります。選択するときは、プロジェクトの要件とチームテクノロジースタックを考慮する必要があります。

Golangは高い並行性タスクにより適していますが、Pythonには柔軟性がより多くの利点があります。 1.Golangは、GoroutineとChannelを介して並行性を効率的に処理します。 2。Pythonは、GILの影響を受けるが、複数の並行性メソッドを提供するスレッドとAsyncioに依存しています。選択は、特定のニーズに基づいている必要があります。

GolangとCのパフォーマンスの違いは、主にメモリ管理、コンピレーションの最適化、ランタイム効率に反映されています。 1)Golangのゴミ収集メカニズムは便利ですが、パフォーマンスに影響を与える可能性があります。

seetgolangforhighperformance andconcurrency、ithyforbackendservicesandnetworkプログラミング、selectthonforrapiddevelopment、datascience、andmachinelearningduetoistsversitydextentextensextensentensiveLibraries。

GolangとPythonにはそれぞれ独自の利点があります。Golangは高性能と同時プログラミングに適していますが、PythonはデータサイエンスとWeb開発に適しています。 Golangは同時性モデルと効率的なパフォーマンスで知られていますが、Pythonは簡潔な構文とリッチライブラリエコシステムで知られています。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

Dreamweaver Mac版
ビジュアル Web 開発ツール

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

WebStorm Mac版
便利なJavaScript開発ツール
