Upgrade to Pro — share decks privately, control downloads, hide ads and more …

k8s源码从头学11课

Avatar for bin Chou bin Chou
April 28, 2023
31

 k8s源码从头学11课

Avatar for bin Chou

bin Chou

April 28, 2023
Tweet

Transcript

  1. 如何看pr 打开 goland 编辑器,在菜单栏找到 Git 下拉框选择 Show Git log 如下图:

    找到你要跳转的pr,⽐如找到我们要学习的第⼀个pr #17,右键选择checkout Revision,你会发现本地⽂件跳转 到⽬标版本,同时编辑器右上⽅会显示修改的⽂件。点击 Changes to 9541867c 会显示3个⽂件夹,这就是这次pr 修改的⽂件列表。
  2. PR #17 Populate 'Kind' fields for all JSON requests. This

    will facilitate better client side UX ⼤意是给所有的资源请求接⼝,返回Kind这个字段,对于客户端更加友好 我们可以在提交的 pr 中给ServiceList 这个结构体中添加了 JsonBase 这个内嵌对象,同时在ServiceRestStroage 中给Kind字段赋值为 cluster#service PR #23 Add test to kubelet, coverage up to 37% ⼤意是给 kubelet 增加测试代码 1. ⾸先将 KubeletServer 结构体中 Kubelet 变成 kubeletInterface 接⼝,⽅便后续测试时候mock // JSONBase is shared by all objects sent to, or returned from the client type JSONBase struct { Kind string `json:"kind,omitempty" yaml:"kind,omitempty"` ID string `json:"id,omitempty" yaml:"id,omitempty"` CreationTimestamp string `json:"creationTimestamp,omitempty" yaml:"creationTimestamp,omitempty"` SelfLink string `json:"selfLink,omitempty" yaml:"selfLink,omitempty"` } // kubeletInterface contains all the kubelet methods required by the server. // For testablitiy. type kubeletInterface interface { GetContainerID(name string) (string, error) GetContainerInfo(name string) (string, error) }
  3. 2. 添加了⼀个 kubelet_server_test.go 测试⽂件,⽤来测试 kubeletServer上的两个接⼝ /container 【获取容器配置】 /containerInfo 【获取容器详情】 这⾥注意

    go语⾔测试 httpServer 的时候,经常会引⽤ httptest包下的 Newserver来创建个临时的服务器。 :::tip 测试您的代码是⼀种很好的做法,可以让开发⼈员有信⼼将其交付⽣产。单元和集成测试⾮常适合测试应⽤程序逻 辑或独⽴的功能⽚段,但在应⽤程序的“边缘”还有其他代码区域更难测试,因为它们处理来⾃第三⽅的传⼊或传出 请求派对。幸运的是,Go 在其标准库中嵌⼊了 httptest 包,这是⼀⼩组结构和函数,可帮助为应⽤程序的这些边 缘创建端到端测试。 参考:https://bignerdranch.com/blog/using-the-httptest-package-in-golang/ 作业地址:https://go.dev/play/p/_DALxIA55dn 作业要求:代码补全,实现单元测试 注意你应该把Handle函数放⼀个名叫handler.go 的⽂件,同时把你的测试函数放到⼀个名叫 handler_test.go的⽂件 你可以尝试在url中传5和3,获取这两个参数相加并返回8 :::